0

こんにちは、JComboBox と JTextField で構成されるインターフェイスを作成しようとしています。JComboBox にラベルを追加するコードを整理しましたが、テキスト フィールドにラベルを追加するのに問題があります。どんな助けでも大歓迎です。

    import javax.swing. *;
    import java.awt.event. *;   
    import java.awt.FlowLayout;
    import java.lang.Math; 

    public class AreaFrame3  extends JFrame
    {  

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

         JPanel panel1 = new JPanel(new FlowLayout()); //set frame layout

         JLabel label1 = new JLabel("Select shape:");

         panel1.add(label1);

         panel1.add(comboBox); 



         JTextField text = new JTextField(10); //create text field


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


         //set JFrame ssize

         frame.setSize(400,250);

         //make JFrame visible. So we can see it

         frame.setVisible(true);

      }

  }   
4

1 に答える 1

2

ここにそれを行う1つの方法があります。すべてのウィジェットをpanel1適切な順序で配置するだけです。

長い目で見れば、これはおそらく保守性があまり高くなく、 よりも優れた LayoutManager が必要になるでしょうが、FlowLayout単に Swing を学びたいだけなら、これは良いスタートになるかもしれません。FlowLayout では不十分だと思われる場合は、LayoutManager のチュートリアルをご覧ください。私の個人的なお気に入りは:BorderLayoutGridBagLayout. MigLayoutも良いかもしれませんが、私は一度も使用したことがなく、JVM の一部ではありません。

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class AreaFrame3 {

    protected void initUI() {
        // 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("Text label:");
        JTextField text = new JTextField(10); // create text field
        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);
        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();
        // make JFrame visible. So we can see it
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new AreaFrame3().initUI();
            }
        });
    }
}
于 2012-12-07T13:03:34.143 に答える