gpt4 book ai didi

java - 如何在 JTextField 中显示淡灰色 "ghost text"?

转载 作者:IT老高 更新时间:2023-10-28 21:04:36 25 4
gpt4 key购买 nike

我不知道我是否得到了正确的名称,但我正在寻找是否有一种特定的方法来实现一个文本字段,以便当它没有焦点并且是空的时,一个微弱的灰色文本字符串显示在字段中。单击该字段时,文本应该消失,就像 StackOverflow 的搜索栏的工作方式一样。我知道我可以更改使用 setForeground() 并关注监听器来完成此操作,但我只是想知道是否有人知道一些可以为我处理此问题的 Java 实现。

最佳答案

对于它的值(value),我发现实际实现它很有趣,所以我想与你分享它(我不是在寻找投票)。

这真的是非侵入性的,因为您所要做的就是调用 new GhostText(textField, "请在此处输入一些文本...");。剩下的代码只是为了让它运行。

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();
}
}

关于java - 如何在 JTextField 中显示淡灰色 "ghost text"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com