为什么我的JButton不会出现,直到applet调整大小并且我的JCombo框在被点击之前是不可见的?

时间:2012-08-02 16:08:47

标签: java swing graphics applet awt

嗨首先,我想说我是网站新手,对编程很感兴趣(主要是图形),我想提前道歉,问任何愚蠢的问题。此外,我还要感谢任何有兴趣抽出时间帮助我的人。

我的问题是,当我尝试通过让某些JButton使某些按钮不可见而其他可见(使其看起来像一个不同的屏幕)来制作菜单时,除非我重新调整窗口大小或最小化,否则一切都不可见。但是,在执行此操作后,从菜单切换到菜单工作完美。此外,我还有一些JComboBox出现,但是在点击之前它们是不可见的或部分不可见的,我不知道它们为什么会这样做。

这个小程序将作为一个工具完成在线工作,以帮助其他孩子化学我开始工作的部分(带有JComboBoxes的部分)是一个单位转换器。我希望在将所有内容正确放置之前让一切正常工作,并且我的一些变量名称可能会让其他人感到有些困惑,所以如果在我的代码中描述一些应该做的事情会让你更容易帮助,请问我和我我会尽力解释我在那里做的事情。

再次感谢

以下是我为applet创建图形的代码:

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);
    }
}

1 个答案:

答案 0 :(得分:2)

  1. 不要将Swing和AWT组件混合在一起。使用所有Swing组件。混合Swing和AWT组件会导致许多问题。
  2. 添加所有组件后调用validate()。这告诉JVM验证组件的布局。它是pack()为框架所做的一部分。
  3. 使用CardLayout在不同的GUI元素之间进行交换。