4

我正在尝试创建一个模拟唱片店的简单 GUI。我还处于起步阶段。

当我尝试添加文本来描述用户在文本字段中输入的内容时遇到了麻烦。

此外,我也无法将每个文本字段定位在自己的行中。换句话说,如果一行中有两个文本字段的空间,那么它会显示在一行中,我试图将每个文本字段都显示在自己的行中。

这是我到目前为止所尝试的:

item2 = new JTextField("sample text");

但是上面的代码只是在文本字段中添加了默认文本,这不是我需要的:/

我提前感谢所有帮助。

public class MyClass extends JFrame{
        private JTextField item1;
        private JTextField item2;

        public MyClass(){
            super("Matt's World of Music");
            setLayout(new FlowLayout());

            item1 = new JTextField();
            item2 = new JTextField();

            add(item1);
            add(item2);

            thehandler handler = new thehandler();

            item1.addActionListener(handler);
            item2.addActionListener(handler);   

        }

    }
4

4 回答 4

5

对于您的第一个问题,您需要使用 JLabel 来显示您的文本。构造函数是这样的:

JLabel label = new JLabel("Your text here");

在 GUI 中工作得非常好。

至于自己做事,我推荐一个 GridLayout。便于使用。

在您的构造函数中,在添加任何内容之前,请执行以下操作:

setLayout(new GridLayout(rows,columns,x_spacing,y_spacing));

x_spacingy_spacing都是确定元素水平和垂直间距的整数。

然后像你做的那样添加。摆弄它,你会得到它的工作。

所以你的决赛看起来像:

setLayout(new GridLayout(2,2,10,10));
add(new JLabel("Text 1"));
add(text1);
add(new JLabel("text 2"));
add(text2);
于 2012-08-26T22:37:06.497 回答
3

您可以只使用JLabel来标记您的文本字段。

    JLabel label1 = new JLabel("Item 1: ");
    add(label1);
    add(item1);

如果您真的想要字段内的文本,您可以使用构造函数设置字段中的文本,然后添加一个 MouseListener 以在单击时清除文本:

    item1 = new JTextField("Text");
    item1.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            if (item1.getText().equals("Text")) // User has not entered text yet
                item1.setText("");
        }
    });

或者,(可能更好)使用 FocusListener:

    item1 = new JTextField("Text");
    item1.addFocusListener(new FocusListener() {
        public void focusGained(FocusEvent e) {
            if (item1.getText().equals("Text")) // User has not entered text yet
                item1.setText("");
        }
        public void focusLost(FocusEvent e) {
            if (item1.getText().equals("")) // User did not enter text
                item1.setText("Text");
        }
    });

至于布局,要强制使用单独的行,请使用Box

    Box itemBox = Box.createVerticalBox();
    itemBox.add(item1);
    itemBox.add(item2);
    add(itemBox);
于 2012-08-26T22:55:06.813 回答
1

制作:

item1 = new JTextField(10);
item2 = new JTextField(10);

这应该解决 JTextField 的宽度问题。开始使用 GridLayout 在一行中显示 JTextField。之后,我强烈推荐使用 MIG Layout http://www.migcalendar.com/miglayout/whitepaper.html

将 JLabel 放在 JTextField 旁边,以描述用户希望在文本字段中输入的内容。

JLabel lbl = new JLabel("Description");

或者您也可以考虑使用 toolTipText:

item1.setToolTipText("This is description");
于 2012-08-26T22:39:04.453 回答
0

For making a form in Java Swing, I always recommend the FormLayout of JGoodies, which is designed to ... create forms. The links contains an example code snippet, which I just copy-pasted here to illustrate how easy it is:

public JComponent buildContent() {
    FormLayout layout = new FormLayout(
            "$label, $label-component-gap, [100dlu, pref]",
            "p, $lg, p, $lg, p");

    PanelBuilder builder = new PanelBuilder(layout);
    builder.addLabel("&Title:",  CC.xy(1, 1));
    builder.add(titleField,      CC.xy(3, 1));
    builder.addLabel("&Author:", CC.xy(1, 3));
    builder.add(auhtorField,     CC.xy(3, 3));
    builder.addLabel("&Price:",  CC.xy(1, 5));
    builder.add(priceField,      CC.xy(3, 5));
    return builder.getPanel();
}

Now for the description:

  • Use a label in front of the textfield to give a very short description
  • You can put a longer description in the textfield as suggested by @Alden. However, if the textfield is for short input, nobody will be able to read the description
  • You can use a tooltip (JComponent#setTooltipText) to put a longer description. Those tooltips also accept basic html which allows some formatting. Drawback of the tooltips is that the user of your application has to 'discover' that feature as there is no clear indication those are available
  • You can put a "help-icon" (like e.g. a question mark) after each text field (use a JButton with only an icon) where on click you show a dialog with a description (e.g. by using the JOptionPane class)
  • You can put one "help-icon" on each form which shows a dialog with a description for all fields.

Note for the dialog suggestion: I wouldn't make it a model one, allowing users to open the dialog and leave it open until they are finished filling in the form

于 2012-08-27T07:27:50.833 回答