java 向文本字段添加标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13762589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:53:52  来源:igfitidea点击:

adding a label to a text field

javaswing

提问by Ikemesit Ansa

Hi I am trying to create an interface consisting of a JComboBox and a JTextField. I have sorted out the code to add a label to the JComboBox but I am having trouble adding a label to the text field. Any help would be appreciated.

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

      }

  }   

回答by Guillaume Polet

Here is one way to do it. Simply put all the widgets in your panel1in appropriate order.

这是一种方法。只需将所有小部件panel1按适当的顺序排列即可。

In the long run this is probably not very much maintainable and you would want to have a better LayoutManager than FlowLayout, but if you just want to learn Swing, this may be a good start. If you feel that FlowLayout is not good enough, take a look at the LayoutManager tutorial. My personal favourites are: BorderLayoutand GridBagLayout. MigLayoutmay also be a good one, but I have never used it and it is not part of the JVM.

从长远来看,这可能不太易于维护,您可能希望拥有比 更好的 LayoutManager FlowLayout,但如果您只想学习 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();
            }
        });
    }
}