在文本字段旁边添加文本以描述用户期望输入的数据

时间:2012-08-26 22:34:04

标签: java swing tooltip layout-manager

我正在尝试创建一个模拟记录存储的简单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 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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);

答案 2 :(得分: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");

答案 3 :(得分:0)

为了在Java Swing中制作表单,我总是推荐JGoodies的FormLayout,它旨在...创建表单。这些链接包含一个示例代码段,我只是在这里复制粘贴,以说明它是多么容易:

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

现在描述:

  • 使用文本字段前面的标签来提供非常简短的说明
  • 您可以按照@Alden的建议在文本字段中添加更长的说明。但是,如果文本字段是短输入,则没有人能够阅读说明
  • 您可以使用工具提示(JComponent#setTooltipText)来进行更长时间的说明。那些工具提示也接受基本的html,允许一些格式化。工具提示的缺点是应用程序的用户必须“发现”该功能,因为没有明确指示可用的功能
  • 您可以在每个文本字段后面添加一个“帮助图标”(例如问号)(仅使用带有图标的JButton),点击它可以显示带描述的对话框(例如,使用JOptionPane类)
  • 您可以在每个表单上放置一个“帮助图标”,其中显示包含所有字段说明的对话框。

注意对话框建议:我不会把它作为模型,允许用户打开对话框并保持打开状态,直到填写完表格