如何在JTextField中显示灰色的“幽灵文本”?

如何在JTextField中显示灰色的“幽灵文本”?,第1张

如何在JTextField中显示灰色的“幽灵文本”?

对于它的价值,我发现实际实现它很有趣,因此我想与您分享(我不是在寻求投票)。

这真的是非侵入性的,因为您要做的就是致电

new GhostText(textField, "Please enter some texthere...");
。剩下的代码只是使它运行。

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5506021.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存