使用JFrame

时间:2012-12-10 22:23:20

标签: java swing

嗨,我基本上放弃了。好的,这就是我想要做的。到目前为止,我已经编写了代码来创建一个包含文本字段和组合框的JFrame。代码应该根据从组合框中选择的形状计算文本字段中输入的区域,并在JFrame上输出结果!

Here's is what the output should look like

到目前为止,这是我的代码。它有点混乱,但任何帮助将非常感激。提前致谢

 import javax.swing. *;
 import java.awt.event. *;   
 import java.awt.FlowLayout;
 import java.lang.Math; 
 public class AreaFrame3  extends JFrame
 {  

     double Area;
     double input;

 public static void main(String[]args)
 {
   //Create array containing shapes
   String[] shapes ={"(no shape selected)","Circle","Equilateral Triangle","Square"};

   //Use combobox to create drop down menu
   JComboBox comboBox=new JComboBox(shapes);
   JLabel label1 = new JLabel("Select shape:");
   JPanel panel1 = new JPanel(new FlowLayout()); //set frame layout

   JLabel label2 = new JLabel("(select shape first)");
   JTextField text = new JTextField(10); //create text field
   text.setEnabled(false);



   panel1.add(label1);
   panel1.add(comboBox);
   panel1.add(label2);
   panel1.add(text);



   JFrame frame=new JFrame("Area Calculator Window");//create a JFrame to put combobox
   frame.setLayout(new FlowLayout()); //set layout
   frame.add(panel1);
   frame.add(text);
   //JButton button = new JButton("GO"); //create GO button
   //frame.add(button);

   //set default close operation for JFrame
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.pack();
   //set JFrame ssize
   frame.setSize(400,250);

   //make JFrame visible. So we can see it
   frame.setVisible(true);


   // public void actionPerformed(ActionEvent e)
    //{

  }

 public void AreaCalc()
 {
    JButton button = new JButton("GO"); //create GO button
   frame.add(button);
   button.addActionListener(
      new ActionListener(){
         public void actionPerformed(ActionEvent e)
        {
           int input = double.parseDouble(text.getText());


        if(e.getSource() == button)
        {              
            String shape = (String).comboBox.getSelectedItem(); 

           if(shape == "(no shape selected)")
           {
             text.setEnabled(false);
           }

          else{
              text.setEnabled(true);
          }

          if(input > 1 && shape == "Circle")
          {
             // label2.getText() = "Enter the radius of the circle: ";
              Area = (Math.PI * (input * input));

          }
        }

         else{}
       }
     }         
    );
  } 
}            

2 个答案:

答案 0 :(得分:3)

我试着理解你在这里做了什么:

panel1.add(label1);
panel1.add(comboBox);
panel1.add(label2);
panel1.add(text); // <---


JFrame frame=new JFrame("Area Calculator Window");//create a JFrame to put combobox
frame.setLayout(new FlowLayout()); //set layout
frame.add(panel1);
frame.add(text); // <---

特别是frame.add(text);panel1.add(text);。不要向JFrame添加文字。使用JPanel

此外,

public class AreaFrame3  extends Frame

使用public class AreaFrame3 extends JFrame,因此您无需创建其他JFrame:

JFrame frame=new JFrame("Area Calculator Window");

类似的东西:

super.setLayout(new FlowLayout()); //set layout
super.add(panel1); 

super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
super.pack();
//set JFrame ssize
super.setSize(400,250);

//make JFrame visible. So we can see it
super.setVisible(true);

最后,我会给你一些启动(这会对你有帮助):

public class FrameExmpl extends JFrame{

private static final long serialVersionUID = 1L;

private        JTabbedPane       tabbedPane;        
private        JPanel            topPanel;
private JTextField               txtf_loadDS_;


public static int valueInt = 0; // responsible for Task status updating
public static Boolean isFinish = false;


public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{ 

    UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );

    FrameExmpl UI_L = new FrameExmpl();

    UI_L.buildGUI();

    UI_L.setVisible(true);
}


public void buildGUI(){

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            dispose();
        }
    });

    setSize(435, 225);
    setLocation(285, 105);  

    setResizable(false);

    topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel);

    txtf_loadDS_ = new JTextField();                
    txtf_loadDS_.setBounds(22, 22, 382, 25);
    topPanel.add(txtf_loadDS_);


    finishBuildGUI();
}


public void finishBuildGUI(){       
    tabbedPane = new JTabbedPane();     
    topPanel.add(tabbedPane, BorderLayout.CENTER);
}
}

enter image description here

答案 1 :(得分:2)

此应用程序存在多个问题,例如从Frame延伸而不是JFrame&amp;尝试从int分配Double.parseDouble。我建议您再次开始构建一个小型但正常工作的应用程序并逐步添加功能,这样就可以更容易地修复错误。