在文本字段中添加标签

时间:2012-12-07 11:54:50

标签: java swing

您好我正在尝试创建一个由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);

      }

  }   

1 个答案:

答案 0 :(得分:2)

这是一种方法。只需按照适当的顺序将所有小部件放在panel1中。

从长远来看,这可能不是很容易维护,你可能希望有一个比FlowLayout更好的LayoutManager,但如果你只是想学习Swing,这可能是一个好的开始。如果您觉得FlowLayout不够好,请查看LayoutManager教程。我个人的最爱是:BorderLayoutGridBagLayoutMigLayout也可能是一个好的,但我从未使用它,它不是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();
            }
        });
    }
}