栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/514961.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号