[英]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_spacing
和y_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: 现在进行描述:
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 工具提示的缺点是应用程序的用户必须“发现”该功能,因为没有明确指示可用的功能 JButton
with only an icon) where on click you show a dialog with a description (eg by using the JOptionPane
class) 您可以在每个文本字段后面添加一个“帮助图标”(例如问号)(使用只带有图标的JButton
),点击它可以显示带描述的对话框(例如,使用JOptionPane
类) 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.