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

最後に、最初にいくつかの tamplate を提供します (これは役に立ちます)。

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

Framefromではなくfrom を拡張し、 fromJFrameを割り当てようとするなど、このアプリケーションには複数の問題があります。小さいながらも動作するアプリケーションの構築を再開し、機能を段階的に追加することをお勧めします。これにより、エラーを修正しやすくなります。intDouble.parseDouble

于 2012-12-10T22:34:18.357 に答える