嗨,首先我想说我是这个网站的新手,对编程(主要是图形)很陌生,我想提前为提出任何愚蠢的问题道歉。此外,我还要感谢任何好心的人,他们在一天中抽出时间来帮助我。
我的问题是,当我试图通过某些JButton使某些按钮不可见,然后使其他按钮可见(使其看起来像一个不同的屏幕)来制作菜单时,除非我重新调整窗口大小或最小化,否则所有内容都是不可见的。然而,完成此操作后,从菜单切换到菜单的操作非常完美。此外,我还有一些JComboBoxes,它们出现了,但在被点击之前要么是不可见的,要么是部分不可见的。我也不知道它们为什么这么做。
当这个小程序作为一个工具来帮助其他孩子学习化学时,它将被放到网上——我首先开始研究的部分(JComboBoxes的部分)是一个单元转换器。我想在把所有东西都放在正确的位置之前让所有东西都正常工作,我的一些变量名可能会让其他人有点困惑,所以如果描述我的代码中应该做什么会让你更容易提供帮助,请问我,我会尽力解释我在那里做什么。
再次感谢
以下是我为小程序创建图形的代码:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;
public class test extends Applet implements ActionListener
{
private int screen = -1;//-1 is value for startup screen
private JComboBox box1, box2, box3, box4, box5, box11, box22, box33, box44, box55;//drop downs for unit converter
private static String[] cVolumeO = {"Ml","L"}; //Unit options for converter
private static String[] cAreaO = {"sqFt","sqMile"};
private static String[] cLengthO = {"ft","Mile"};
private static String[] cTempO = {"C","F","K"};
private static String[] cChemO = {"Moles","Grams"}; //end of unit options for converter
private static String[] filename;//store pic filenames
private Icon[] pics;//stores applet pics
private JLabel picture;
private Button MainMenu;
private Button periodicTable, element, solvers, balancer, fChart, converter; //main menu buttons
private Button Continue; //start up screen
private TextField dialog;
private TextField cVolume, cArea, cLength, cTemp, cChem; //converter entry fields
private TextField volumeAnswer, areaAnswer, lengthAnswer, tempAnswer, chemAnswer;//converter answer fields
public void init(){
setBackground(new Color(10,10,205));
setSize(500, 500);
setLayout(new FlowLayout());
instanceVarAssigner();//sets all variables to their correct values (it's basically a constructor)
createStartScreen_();//sets up default screen by making all invisible except continue button + default words and picture
assignLocation();//positions all of the items to their proper locations
add(Continue); //adds all buttons to the applet screen
add(periodicTable);
add(element);
add(solvers);
add(balancer);
add(fChart);
add(converter);
add(MainMenu);
add(box1);
add(box11);
add(box2);
add(box22);
add(box3);
add(box33);
add(box4);
add(box44);
add(box5);
add(box55);
add(cVolume);
add(cArea);
add(cLength);
add(cTemp);
add(cChem);
add(volumeAnswer);
add(areaAnswer);
add(lengthAnswer);
add(tempAnswer);
add(chemAnswer);
// adds functionality to all the buttons
periodicTable.addActionListener(this);
element.addActionListener(this);
solvers.addActionListener(this);
balancer.addActionListener(this);
fChart.addActionListener(this);
converter.addActionListener(this);
MainMenu.addActionListener(this);
Continue.addActionListener(this);
box1.addActionListener(this);
box2.addActionListener(this);
box3.addActionListener(this);
box4.addActionListener(this);
box5.addActionListener(this);
repaint();
}//end init();
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.drawString(dialog.getText(),20,100);
if(screen==0){//Main Menu
mainSetup();
}
else if(screen==1){//PeriodicTable
ptSetup();
}
else if(screen==2){//Element Finder/Info
elementSetup();
}
else if(screen==3){//Solvers
solverSetup();
}
else if(screen==4){//Balancer
balancerSetup();
}
else if(screen==5){//fChart
fChartSetup();
}
else if(screen==6){//Converter
converterSetup();
}
}//end paint();
private void solverSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void elementSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void balancerSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void fChartSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void converterSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
box1.setVisible(true);
box2.setVisible(true);
box3.setVisible(true);
box4.setVisible(true);
box5.setVisible(true);
box11.setVisible(true);
box22.setVisible(true);
box33.setVisible(true);
box44.setVisible(true);
box55.setVisible(true);
cVolume.setVisible(true);
cArea.setVisible(true);
cLength.setVisible(true);
cTemp.setVisible(true);
cChem.setVisible(true);
volumeAnswer.setVisible(true);
areaAnswer.setVisible(true);
lengthAnswer.setVisible(true);
tempAnswer.setVisible(true);
chemAnswer.setVisible(true);
}
private void ptSetup() {
periodicTable.setVisible(false);
element.setVisible(false);
solvers.setVisible(false);
balancer.setVisible(false);
fChart.setVisible(false);
converter.setVisible(false);
MainMenu.setVisible(true);
}
private void mainSetup() {
periodicTable.setVisible(true);
element.setVisible(true);
solvers.setVisible(true);
balancer.setVisible(true);
fChart.setVisible(true);
converter.setVisible(true);
MainMenu.setVisible(false);
}
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource() == element){
dialog.setText("element was pushed.");
screen=2;
repaint();
}
else if(evt.getSource() == periodicTable){
dialog.setText("pt was pushed.");
screen=1;
repaint();
}
else if(evt.getSource() == solvers){
dialog.setText("s was pushed.");
screen=3;
repaint();
}
else if(evt.getSource() == balancer){
dialog.setText("b was pushed.");
screen=4;
repaint();
}
else if(evt.getSource() == fChart){
dialog.setText("fC was pushed.");
screen=5;
repaint();
}
else if(evt.getSource() == converter){
dialog.setText("c was pushed.");
screen=6;
repaint();
}
else if(evt.getSource() == MainMenu){
createStartScreen_();//refreshes shutting off of all things so that the code doesnt need to be repeated.
dialog.setText("Main Menu");
Continue.setVisible(false);
screen=0;
repaint();
}
else if(evt.getSource() == Continue){
dialog.setText("Main Menu");
screen=0;
Continue.setVisible(false);
repaint();
}
}//end actionPreformed();
private void instanceVarAssigner(){
MainMenu=new Button("Main Menu");
periodicTable = new Button("Peoridic Table");
element = new Button("Element Finder/Info");
solvers = new Button("Solvers");
balancer = new Button("Equation Balancer");
fChart = new Button("Formula Chart");
converter = new Button("Unit Converter");
Continue = new Button("Continue");
box1 = new JComboBox(cVolumeO);//converter items
box11 = new JComboBox(cVolumeO);
box2 = new JComboBox(cAreaO);
box22 = new JComboBox(cAreaO);
box3 = new JComboBox(cLengthO);
box33 = new JComboBox(cLengthO);
box4 = new JComboBox(cTempO);
box44 = new JComboBox(cTempO);
box5 = new JComboBox(cChemO);
box55 = new JComboBox(cChemO);
volumeAnswer = new TextField("0");
areaAnswer = new TextField("0");
lengthAnswer = new TextField("0");
tempAnswer = new TextField("0");
chemAnswer = new TextField("0");
cVolume = new TextField("0");
cArea = new TextField("0");
cLength = new TextField("0");
cTemp = new TextField("0");
cChem = new TextField("0"); //end of converter items
dialog = new TextField("Homework Helper: Chemistry Edition V1.0 by BMCA Tech. Press Continue to begin...",35);
preventChanges();//makes all answer fields in app uneditable
}
private void createStartScreen_(){//sets all buttons to false other than continue which is set to true and displays default pic and text.
Continue.setVisible(true);
dialog.setText("Homework Helper: Chemistry Edition V1.0 by BMCA Tech. Press Continue to begin...");
periodicTable.setVisible(false);
MainMenu.setVisible(false);
converter.setVisible(false);
fChart.setVisible(false);
balancer.setVisible(false);
solvers.setVisible(false);
element.setVisible(false);
box1.setVisible(false);
box2.setVisible(false);
box3.setVisible(false);
box4.setVisible(false);
box5.setVisible(false);
box11.setVisible(false);
box22.setVisible(false);
box33.setVisible(false);
box44.setVisible(false);
box55.setVisible(false);
cVolume.setVisible(false);
cArea.setVisible(false);
cLength.setVisible(false);
cTemp.setVisible(false);
cChem.setVisible(false);
volumeAnswer.setVisible(false);
areaAnswer.setVisible(false);
lengthAnswer.setVisible(false);
tempAnswer.setVisible(false);
chemAnswer.setVisible(false);
}//end createStartScreen
private void assignLocation(){
MainMenu.setLocation(10, 10);
box1.setLocation(10, 50);
}
private void preventChanges(){
volumeAnswer.setEditable(false);
areaAnswer.setEditable(false);
lengthAnswer.setEditable(false);
tempAnswer.setEditable(false);
chemAnswer.setEditable(false);
}
}
- 不要将Swing和AWT组件混合在一起使用所有Swing组件。混合Swing和AWT组件会导致许多问题
- 添加所有组件后调用
validate()
。这告诉JVM验证组件的布局。这是pack()
对帧所做的工作的一部分 - 使用
CardLayout
在不同的GUI元素之间进行交换