0

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

这是输出的样子

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

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

2 回答 2

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

在此处输入图像描述

于 2012-12-10T22:32:16.517 回答
2

此应用程序存在多个问题,例如扩展 fromFrame而不是JFrame& 尝试分配intfrom Double.parseDouble。我建议您重新开始构建一个小而有效的应用程序并逐步添加功能,这样更容易修复错误。

于 2012-12-10T22:34:18.357 回答