java JTextField 中的示例文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5044628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Example text in JTextField
提问by startoftext
I am looking for a way to put example text into a swing JTextField and have it grayed out. The example text should then disappear as soon as any thing is entered into that text field. Some what similar to what stackoverflow does when a user is posting a question with the title field.
我正在寻找一种将示例文本放入摆动 JTextField 并将其灰显的方法。一旦在该文本字段中输入任何内容,示例文本就会消失。一些类似于当用户在标题字段中发布问题时 stackoverflow 所做的事情。
I would like it if it was already a extended implementation of JTextField so that I can just drop it in as a simple replacement. Anything from swingx would work. I guess if there is not an easy way to do this my option will probably be to override the paint method of JTextField do something that way maybe.
如果它已经是 JTextField 的扩展实现,我会喜欢它,这样我就可以将它作为简单的替代品放入其中。来自 Swingx 的任何东西都可以。我想如果没有一种简单的方法可以做到这一点,我的选择可能是覆盖 JTextField 的绘制方法,以这种方式做一些事情。
Thanks
谢谢
采纳答案by camickr
The Text Promptclass provides the required functionality without using a custom JTextField.
该文字提示类提供所需的功能,而无需使用自定义的JTextField。
It allows you to specify a prompt that is displayed when the text field is empty. As soon as you type text the prompt is removed.
它允许您指定在文本字段为空时显示的提示。只要您输入文本,提示就会被删除。
The prompt is actually a JLabel
so you can customize the font, foreground etc..:
提示实际上是一个,JLabel
因此您可以自定义字体、前景等:
JTextField tf7 = new JTextField(10);
TextPrompt tp7 = new TextPrompt("First Name", tf7);
tp7.setForeground( Color.RED );
回答by Daniel Teply
If you can use external librairies, the Swing components from Jide softwarehave what you are looking for; it's called LabeledTextField (javadoc) and it's part of the JIDE Common Layer (Open Source Project) - which is free. It's doing what mklhmnn suggested.
如果您可以使用外部库,那么 Jide软件的 Swing 组件就有您想要的;它被称为 LabeledTextField ( javadoc),它是 JIDE 公共层(开源项目)的一部分——它是免费的。它正在按照 mklhmnn 的建议进行操作。
回答by Hovercraft Full Of Eels
How about initialize the text field with default text and give it a focus listener such that when focus is gained, if the text .equals the default text, call selectAll() on the JTextField.
如何使用默认文本初始化文本字段并为其提供焦点侦听器,以便在获得焦点时,如果文本 .equals 为默认文本,则调用 JTextField 上的 selectAll()。
回答by jzd
Rather than overriding, put a value in the field and add a KeyListener
that would remove the value when a key stroke is registered. Maybe also have it change the foreground.
而不是覆盖,在该字段中放置一个值并添加一个KeyListener
将在注册击键时删除该值的值。也许也让它改变前景。
You could wrap this up into your own custom JTextField
class that would take the default text in a constructor.
您可以将其包装到您自己的自定义JTextField
类中,该类将在构造函数中采用默认文本。
回答by ignis
private JLabel l;
JPromptTextField(String prompt) {
l = new JLabel(prompt, SwingConstants.CENTER);
l.setForeground(Color.GRAY);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (this.getText().length() == 0) {
// Reshape the label if needed, then paint
final Rectangle mine = this.getBounds();
final Rectangle its = l.getBounds();
boolean resized = (mine.width != its.width) || (mine.height != its.height);
boolean moved = (mine.x != its.x) || (mine.y != its.y);
if (resized || moved)
l.setBounds(mine);
l.paint(g);
}
}
回答by Mot
You can't do that with a plain text field, but you can put a disabled JLabel
on top of the JTextField
and hide it if the text field gets the focus.
您不能使用纯文本字段执行此操作,但您可以JLabel
在 顶部放置禁用JTextField
并在文本字段获得焦点时将其隐藏。
回答by Costis Aivalis
Do it like this:
像这样做:
Define the string with the initial text you like and set up your TextField:
String initialText = "Enter your initial text here"; jTextField1.setText(initialText);
Add a Focus Listener to your TextField, which selects the entire contents of the TextField if it still has the initial value. Anything you may type in will replace the entire contents, since it is selected.
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { if (jTextField1.getText().equals(initialText)) { jTextField1.selectAll(); } } });
使用您喜欢的初始文本定义字符串并设置您的 TextField:
String initialText = "Enter your initial text here"; jTextField1.setText(initialText);
将焦点侦听器添加到您的 TextField,如果它仍然具有初始值,它将选择 TextField 的全部内容。您可以输入的任何内容都将替换整个内容,因为它已被选中。
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(java.awt.event.FocusEvent evt) { if (jTextField1.getText().equals(initialText)) { jTextField1.selectAll(); } } });