【问题标题】:Adding text next to a textfield to describe what data the user is expected to enter into it在文本字段旁边添加文本以描述用户希望输入的数据
【发布时间】:2012-08-26 22:34:03
【问题描述】:

我正在尝试创建一个简单的 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);   

        }

    }

【问题讨论】:

  • 看来这个“问题”(文中没有一个“?”)由两个完全不同的问题组成,应该在两个完全不同的帖子上处理。
  • public class MyClass extends JFrame{ 最好是 public class RecordStore{ 1) 使用描述性名称,即使是测试/一次性代码。如果您愿意,可以命名为 RecordStoreTestRecordStoreTest01,但要使其有意义。 2)不要扩展框架,而是保留对一个的引用。 ——当我在这里的时候。在 JPanel 中创建主 GUI 并将其添加到框架中。但也不要扩展面板,只引用一个。

标签: java swing tooltip layout-manager


【解决方案1】:

对于您的第一个问题,您需要使用 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);

【讨论】:

    【解决方案2】:

    您可以只使用 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);
    

    【讨论】:

    • +1 for “或者,(可能更好)使用 FocusListener:” 我通常使用键盘而不是鼠标。 “点击”不好!
    • 如果您将FocusListener 与在focusGained 上调用selectAll 结合使用,它甚至会变得用户友好。否则用户总是必须手动选择和删除描述
    • @Robin 真的吗?不会 setText("");立即清场?
    【解决方案3】:

    制作:

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

    【讨论】:

      【解决方案4】:

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

      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),单击时会显示带有说明的对话框(例如,使用 @ 987654327@班级)
      • 您可以在每个表单上放置一个“帮助图标”,它会显示一个包含所有字段说明的对话框。

      对话框建议的注意事项:我不会让它成为模型,允许用户打开对话框并保持打开状态,直到他们完成填写表格

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-07
        • 2014-09-02
        • 2021-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多