如何在JTextField中显示灰色的“幽灵文本”?
我不知道它的名字是否正确,但是我想看看是否有一种特定的方法来实现文本字段,以便当它没有焦点且为空时,出现一个淡淡的灰色字符串文本显示在该字段中。单击该字段时,文本应该消失,就像StackOverflow之类的搜索栏的工作方式一样。我知道我可以更改使用方法setForeground()
和焦点侦听器来完成此操作,但是我只是想知道是否有人知道某些Java实现可以帮助我解决此问题。
回答:
对于它的价值,我发现实际实现它很有趣,因此我想与您分享(我不是在寻求投票)。
这真的是非侵入性的,因为您要做的就是致电new GhostText(textField, "Please enter some text
here...");。剩下的代码只是使它运行。
import java.awt.Color;import java.awt.Dimension;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class Test {
public static class GhostText implements FocusListener, DocumentListener, PropertyChangeListener {
private final JTextField textfield;
private boolean isEmpty;
private Color ghostColor;
private Color foregroundColor;
private final String ghostText;
protected GhostText(final JTextField textfield, String ghostText) {
super();
this.textfield = textfield;
this.ghostText = ghostText;
this.ghostColor = Color.LIGHT_GRAY;
textfield.addFocusListener(this);
registerListeners();
updateState();
if (!this.textfield.hasFocus()) {
focusLost(null);
}
}
public void delete() {
unregisterListeners();
textfield.removeFocusListener(this);
}
private void registerListeners() {
textfield.getDocument().addDocumentListener(this);
textfield.addPropertyChangeListener("foreground", this);
}
private void unregisterListeners() {
textfield.getDocument().removeDocumentListener(this);
textfield.removePropertyChangeListener("foreground", this);
}
public Color getGhostColor() {
return ghostColor;
}
public void setGhostColor(Color ghostColor) {
this.ghostColor = ghostColor;
}
private void updateState() {
isEmpty = textfield.getText().length() == 0;
foregroundColor = textfield.getForeground();
}
@Override
public void focusGained(FocusEvent e) {
if (isEmpty) {
unregisterListeners();
try {
textfield.setText("");
textfield.setForeground(foregroundColor);
} finally {
registerListeners();
}
}
}
@Override
public void focusLost(FocusEvent e) {
if (isEmpty) {
unregisterListeners();
try {
textfield.setText(ghostText);
textfield.setForeground(ghostColor);
} finally {
registerListeners();
}
}
}
@Override
public void propertyChange(PropertyChangeEvent evt) {
updateState();
}
@Override
public void changedUpdate(DocumentEvent e) {
updateState();
}
@Override
public void insertUpdate(DocumentEvent e) {
updateState();
}
@Override
public void removeUpdate(DocumentEvent e) {
updateState();
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
init();
}
});
}
public static void init() {
JFrame frame = new JFrame("Test ghost text");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JTextField textField = new JTextField();
JButton button = new JButton("Grab focus");
GhostText ghostText = new GhostText(textField, "Please enter some text here...");
textField.setPreferredSize(new Dimension(300, 24));
panel.add(textField);
panel.add(button);
frame.add(panel);
frame.pack();
frame.setVisible(true);
button.grabFocus();
}
}
以上是 如何在JTextField中显示灰色的“幽灵文本”? 的全部内容, 来源链接: utcz.com/qa/415075.html