java swing JTextField 设置 PlaceHolder
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16213836/
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
java swing JTextField set PlaceHolder
提问by Renish Khunt
I create a JTextField and now i want to set the placeholder on that JTextField i don't Know how to set the placeholder on the JTextField? Please Help How to set Placeholder text on JTextField
我创建了一个 JTextField,现在我想在该 JTextField 上设置占位符我不知道如何在 JTextField 上设置占位符?请帮助如何在 JTextField 上设置占位符文本
JTextField database=new JTextField("Enter Data Base Name");
database.setPreferredSize(database.getPreferredSize());
database.setText("");
That is my code to text field now in that code i want to set placeholder how to set a placeholder on that JTextField
那是我现在在该代码中的文本字段代码我想设置占位符如何在该 JTextField 上设置占位符
采纳答案by Peter Tseng
Try this class:
试试这个类:
package playground;
import java.awt.*;
import javax.swing.*;
import javax.swing.text.Document;
@SuppressWarnings("serial")
public class PlaceholderTextField extends JTextField {
public static void main(final String[] args) {
final PlaceholderTextField tf = new PlaceholderTextField("");
tf.setColumns(20);
tf.setPlaceholder("All your base are belong to us!");
final Font f = tf.getFont();
tf.setFont(new Font(f.getName(), f.getStyle(), 30));
JOptionPane.showMessageDialog(null, tf);
}
private String placeholder;
public PlaceholderTextField() {
}
public PlaceholderTextField(
final Document pDoc,
final String pText,
final int pColumns)
{
super(pDoc, pText, pColumns);
}
public PlaceholderTextField(final int pColumns) {
super(pColumns);
}
public PlaceholderTextField(final String pText) {
super(pText);
}
public PlaceholderTextField(final String pText, final int pColumns) {
super(pText, pColumns);
}
public String getPlaceholder() {
return placeholder;
}
@Override
protected void paintComponent(final Graphics pG) {
super.paintComponent(pG);
if (placeholder == null || placeholder.length() == 0 || getText().length() > 0) {
return;
}
final Graphics2D g = (Graphics2D) pG;
g.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(getDisabledTextColor());
g.drawString(placeholder, getInsets().left, pG.getFontMetrics()
.getMaxAscent() + getInsets().top);
}
public void setPlaceholder(final String s) {
placeholder = s;
}
}
回答by camickr
If I understand the question, Text Promptshows one way you can do this.
如果我理解了这个问题,Text Prompt 会显示一种您可以执行此操作的方法。
回答by Mahfuz Ahmed
So simple use a swingx jarthen like this
如此简单地使用一个swingx jar然后像这样
JTextArea txtMSISDNList = new JTextArea();
PromptSupport.setPrompt("01197585960,01197585961", txtMSISDNList);
回答by Graywolf
JTextField searchText;
...
...
In constructor:
在构造函数中:
searchText = new JTextField("Search");
searchText.setForeground(Color.GRAY);
searchText.addFocusListener(new FocusListener() {
@Override
public void focusGained(FocusEvent e) {
if (searchText.getText().equals("Search")) {
searchText.setText("");
searchText.setForeground(Color.BLACK);
}
}
@Override
public void focusLost(FocusEvent e) {
if (searchText.getText().isEmpty()) {
searchText.setForeground(Color.GRAY);
searchText.setText("Search");
}
}
});