简体   繁体   English

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

[英]Adding text next to a textfield to describe what data the user is expected to enter into it

I am trying to create a simple GUI that simulates a record store. 我正在尝试创建一个模拟记录存储的简单GUI。 I am still in the beginning stages. 我还处于起步阶段。

I am running into trouble when I try to add text to describe what the user is expected to enter in the text field. 当我尝试添加文本来描述用户期望在文本字段中输入的内容时,我遇到了麻烦。

In addition, I am also having trouble positioning every textfield on its own line. 另外,我也无法在自己的行上定位每个文本字段。 In other words if there is space for two textfields in one line, then it displays in one line, and I am trying to display every text field on its own line. 换句话说,如果一行中有两个文本字段的空间,那么它将显示在一行中,并且我试图在其自己的行上显示每个文本字段。

This is what I tried so far: 这是我到目前为止所尝试的:

item2 = new JTextField("sample text");

However the code above just adds default text within the text field, which is not what I need :/ 但是上面的代码只是在文本字段中添加了默认文本,这不是我需要的:/

I appreciate all the help in advance. 我提前感谢所有的帮助。

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

        }

    }

For your first problem, you need to use a JLabel to display your text. 对于第一个问题,您需要使用JLabel来显示文本。 The constructor is like this: 构造函数是这样的:

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

Works really well in GUI. 在GUI中工作得很好。

As for getting things on their own lines, I recommend a GridLayout. 至于自己的东西,我推荐一个GridLayout。 Easy to use. 易于使用。

In your constructor, before adding anything, you do: 在构造函数中,在添加任何内容之前,您可以:

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

x_spacing and y_spacing are both integers that determine the space between elements horizontally and vertically. x_spacingy_spacing都是整数,用于水平和垂直确定元素之间的空间。

Then add like you have done. 然后添加就像你做的那样。 Fiddle around with it and you'll get it worked out. 摆弄它,你会得到它。

So your final would look like: 所以你的决赛看起来像:

setLayout(new GridLayout(2,2,10,10));
add(new JLabel("Text 1"));
add(text1);
add(new JLabel("text 2"));
add(text2);

You could just use a JLabel to label your textfields. 您可以使用JLabel标记文本字段。

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

If you really want text inside the fields, you could set the text in the field with the constructor, and then add a MouseListener to clear the text on click: 如果您确实需要字段内的文本,可以使用构造函数在字段中设置文本,然后添加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("");
        }
    });

Or, (probably better) use a FocusListener: 或者,(可能更好)使用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");
        }
    });

As for layout, to force a separate line, you use use a Box . 至于布局,要强制单独一行,您可以使用Box

    Box itemBox = Box.createVerticalBox();
    itemBox.add(item1);
    itemBox.add(item2);
    add(itemBox);

Make: 使:

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

that should solve problem with width of JTextField. 这应该解决JTextField的宽度问题。 For beginning use GridLayout to display JTextField in one line. 首先使用GridLayout在一行中显示JTextField。 After that I strongly recomend using of MIG Layout http://www.migcalendar.com/miglayout/whitepaper.html . 之后,我强烈建议使用MIG Layout http://www.migcalendar.com/miglayout/whitepaper.html

put JLabel next to JTextField to describe what the user is expected to enter in the text field. 将JLabel放在JTextField旁边,以描述用户期望在文本字段中输入的内容。

JLabel lbl = new JLabel("Description");

or you could also consider using of toolTipText: 或者你也可以考虑使用toolTipText:

item1.setToolTipText("This is description");

For making a form in Java Swing, I always recommend the FormLayout of JGoodies, which is designed to ... create forms. 为了在Java Swing中制作表单,我总是推荐JGoodies的FormLayout ,它旨在...创建表单。 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. 您可以按照@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. 您可以使用工具提示( JComponent#setTooltipText )来提供更长的描述。 Those tooltips also accept basic html which allows some formatting. 那些工具提示也接受基本的html,允许一些格式化。 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 eg a question mark) after each text field (use a JButton with only an icon) where on click you show a dialog with a description (eg by using the JOptionPane class) 您可以在每个文本字段后面添加一个“帮助图标”(例如问号)(使用只带有图标的JButton ),点击它可以显示带描述的对话框(例如,使用JOptionPane类)
  • 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 注意对话框建议:我不会把它作为模型,允许用户打开对话框并保持打开状态直到填写完表格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户在字段中输入时,在文本字段中添加$符号 - Adding $ sign into the textfield when the user enter in the field 按空格读取拆分输入,用户在文本字段中输入 - read split input by space enter by user in textfield 使用相同的按钮在文本字段中输入不同的文本 - Using same button to enter different text in textfield 无法使用JTextField在文本旁边添加文本字段 - Unable to add textfield next to text using JTextField 当用户输入数据/擦除文本时,在TextField中显示/消失默认文本 - Display/disappear default text in TextField when user enters data/erase the text 使用 TextField 时在 JavaFX 中添加文本区域 - Adding Text Area in JavaFX when using TextField 创建仅允许用户在Java中输入int / double的文本字段(或类似文本)的简单方法是什么? - What is a simple way to create a text field (or such) that only allows the user to enter ints/doubles in Java? 无法在文本字段中输入数据 - Not able to enter data in the Text field 按下Enter键即可将a移至下一个文本字段,例如Tab键? - Hitting enter to move a to next text field, like tab key? 如何使用Java中的下一步按钮从文本字段中的数据库移动数据 - How to move data from database in textfield using next button in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM