Java文本框设置灰色_如何在JTextField中显示灰色的“幽灵文本”?
小编典典
对于它的价值,我发现实际实现它很有趣,因此我想与您分享(我不是在寻求投票)。
这真的是非侵入性的,因为您要做的就是致电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();
}
}
2020-09-16
Java文本框设置灰色_如何在JTextField中显示灰色的“幽灵文本”?相关推荐
- python 获取文本框内容_如何在python中单击按钮时获取文本框中的文本?
我可以让您的代码在单击按钮时显示文本框中的文本: 这就是你要达到的目的吗?您没有提到是否出现错误,但我确实发现您的代码存在两个问题:Button.py脚本中似乎存在一些缩进问题.在 对于changeT ...
- Java错误提示框口怎么使用_如何在Swing中显示错误消息对话框?
以下示例展示了如何在基于swing的应用程序中显示错误消息警告. 使用以下API - JOptionPane - 创建标准对话框. JOptionPane.showMessageDialog() - ...
- txt文本变为粗体_如何在PHP中使文本变为粗体?
txt文本变为粗体 Sometimes we might want to display text with style. That it's font, color, make it bold, i ...
- java确认对话框否_如何在Swing中显示是/否和取消选项的确认对话框?
下面的示例展示如何在基于swing的应用程序中显示带有是,否和取消选项的确认对话框. 使用以下API - JOptionPane - 创建标准对话框. JOptionPane.showConfirmD ...
- finereport字段显示设置_如何在Excel中显示和编辑中文拼音字段
有时候遇到一些生僻字需要进行注音,或者是其他一些特殊场景,都会使用到拼音的功能.下面就来介绍一下如何使用这一功能. 编辑和显示拼音 1.选中目标单元格,然后点击工具栏上的"显示或隐藏拼音字段 ...
- java ncso发送电邮_如何在Windows中从命令行发送电子邮件(无需额外的软件)
java ncso发送电邮 In Windows there is no way to natively send mail from the Command Prompt, but because ...
- java怎么把数组清空_如何在JavaScript中清空数组?
有没有一种方法可以清空数组,如果可以的话,可以使用.remove()吗? 例如, A = [1,2,3,4]; 我该如何清空? #1楼 清除现有数组A : 方法1 (这是我对问题的原始回答) A = ...
- java多边形晕线的方法_如何在OpenCV中绘制一组封闭的多边形曲线,将每个线段表示为不同的颜色(即在彩虹色空间中)?...
我正在学习使用cv2.approxPolyDP函数将OpenCV轮廓分割成更简单更相关的曲线 . 我想为自己说明这一点,以便更好地了解正在发生的事情 . 我越来越近了,cv2.approxPolyDP ...
- python中如何表示非_如何在Python中显示非ASCII字符?
我用这种方式使用python shell: >>> s = 'Ã' >>> s '\xc3' 如何打印s变量以显示字符_????这是第一个也是最简单的问题.实际上, ...
最新文章
- 如何给机房的服务器安装系统,云机房服务器系统安装
- Linux文本搜索工具grep
- 使用java的html解析器jsoup和jQuery实现一个自动重复抓取任意网站页面指定元素的web应用...
- python三酷猫_洛克王国三代酷猫登场 冰水酷猫解析
- VUE:父子组件间传参、子组件传值给父组件、父组件传值给子组件
- labview linux 内核 不匹配,Linux CentOS7(或Ubuntu)中安装NI-VISA后一打开范例Simple Serial.vi就闪退,LabVIEW就崩溃。...
- 算法总结之 在单链表中删除指定值的节点
- 整理一个双向链表list.h
- Sphinx/Coreseek 4.1 跑 buildconf.sh 一个错误,无法生成configure档
- python求解LeetCode习题Fraction to Recurring Decimal
- C#与SQL实现医院信息管理系统
- Openstack Kilo安装错误汇总
- 毕业设计/论文答辩演讲稿通用模板
- abaqus .cae文件默认程序设置
- 南京柳树湾与云南汉族人
- 程序江湖事——docker江湖缘
- 【抽样调查】CH3 分层随机抽样
- html格式显示图标异常,HTM或HTML图标变成无法显示和识别的解决方法大全
- leetcode每日一题第三十二天-剑指 Offer 65. 不用加减乘除做加法(easy??middle了吧)
- linux系统管理及配置
热门文章
- 前端 js jQuery ajax实现文件流下载, 下载doc,xsl等文件内容乱码问题
- vue实现元素周期表,附赠元素周期表静态数据
- iPhone开发环境
- 2022年湖北省科技进步奖详细解答,该奖项申报条件以及奖励补贴具体情况解析
- 目前智能门锁的发展,主要面临哪些挑战?
- 网络直播步入“剩者为王”的时代,三大层面将成决定性因素
- 思岚A1激光雷达windows系统与ROS系统中的使用
- 太牛逼!这款软件几乎可以操作所有的数据库!
- MBD——Bus使用全解
- oracle快速统计表条数_oracle统计表的所有行数(原创)