具有input提示的Java JTextField

我想给我的JTextField添加一个提示值。 它应该看起来像<input type="text" title="bla"> Firefox渲染。 这将在后台创build一个带有文本“bla”的编辑字段。 如果文本框具有焦点标题,则文本消失,并且如果用户离开编辑框而没有文本,则重新出现。

有一个(免费)摆动组件做这样的事情吗?

看看这个: http : //code.google.com/p/xswingx/

顺便说一下,自己来实现它并不是很困难。 一些听众和自定义渲染器和瞧。

你可以创build自己的:

 import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.*; public class Main { public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setLayout(new BorderLayout()); final JTextField textFieldA = new HintTextField("A hint here"); final JTextField textFieldB = new HintTextField("Another hint here"); frame.add(textFieldA, BorderLayout.NORTH); frame.add(textFieldB, BorderLayout.CENTER); JButton btnGetText = new JButton("Get text"); btnGetText.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String message = String.format("textFieldA='%s', textFieldB='%s'", textFieldA.getText(), textFieldB.getText()); JOptionPane.showMessageDialog(frame, message); } }); frame.add(btnGetText, BorderLayout.SOUTH); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setVisible(true); frame.pack(); } } class HintTextField extends JTextField implements FocusListener { private final String hint; private boolean showingHint; public HintTextField(final String hint) { super(hint); this.hint = hint; this.showingHint = true; super.addFocusListener(this); } @Override public void focusGained(FocusEvent e) { if(this.getText().isEmpty()) { super.setText(""); showingHint = false; } } @Override public void focusLost(FocusEvent e) { if(this.getText().isEmpty()) { super.setText(hint); showingHint = true; } } @Override public String getText() { return showingHint ? "" : super.getText(); } } 

如果你还在Java 1.5中,用this.getText().length() == 0replacethis.getText().isEmpty()

这是一个单一的复制/粘贴解决scheme:

 import java.awt.Color; import java.awt.Graphics; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; import javax.swing.plaf.basic.BasicTextFieldUI; import javax.swing.text.JTextComponent; public class HintTextFieldUI extends BasicTextFieldUI implements FocusListener { private String hint; private boolean hideOnFocus; private Color color; public Color getColor() { return color; } public void setColor(Color color) { this.color = color; repaint(); } private void repaint() { if(getComponent() != null) { getComponent().repaint(); } } public boolean isHideOnFocus() { return hideOnFocus; } public void setHideOnFocus(boolean hideOnFocus) { this.hideOnFocus = hideOnFocus; repaint(); } public String getHint() { return hint; } public void setHint(String hint) { this.hint = hint; repaint(); } public HintTextFieldUI(String hint) { this(hint,false); } public HintTextFieldUI(String hint, boolean hideOnFocus) { this(hint,hideOnFocus, null); } public HintTextFieldUI(String hint, boolean hideOnFocus, Color color) { this.hint = hint; this.hideOnFocus = hideOnFocus; this.color = color; } @Override protected void paintSafely(Graphics g) { super.paintSafely(g); JTextComponent comp = getComponent(); if(hint!=null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))){ if(color != null) { g.setColor(color); } else { g.setColor(comp.getForeground().brighter().brighter().brighter()); } int padding = (comp.getHeight() - comp.getFont().getSize())/2; g.drawString(hint, 2, comp.getHeight()-padding-1); } } @Override public void focusGained(FocusEvent e) { if(hideOnFocus) repaint(); } @Override public void focusLost(FocusEvent e) { if(hideOnFocus) repaint(); } @Override protected void installListeners() { super.installListeners(); getComponent().addFocusListener(this); } @Override protected void uninstallListeners() { super.uninstallListeners(); getComponent().removeFocusListener(this); } } 

像这样使用它:

 TextField field = new JTextField(); field.setUI(new HintTextFieldUI("Search", true)); 

请注意,它发生在protected void paintSafely(Graphics g)

这里有一个简单的方法,在任何L&F看起来都不错:

 public class HintTextField extends JTextField { public HintTextField(String hint) { _hint = hint; } @Override public void paint(Graphics g) { super.paint(g); if (getText().length() == 0) { int h = getHeight(); ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); Insets ins = getInsets(); FontMetrics fm = g.getFontMetrics(); int c0 = getBackground().getRGB(); int c1 = getForeground().getRGB(); int m = 0xfefefefe; int c2 = ((c0 & m) >>> 1) + ((c1 & m) >>> 1); g.setColor(new Color(c2, true)); g.drawString(_hint, ins.left, h / 2 + fm.getAscent() / 2 - 2); } } private final String _hint; } 

对于任何Swing组件(即扩展JComponent的任何东西),可以调用setToolTipText(String)方法。

欲了解更多信息,请参考以下链接:

  • API文档setToolTipText
  • “如何使用工具提示”教程

看看WebLookAndFeel在https://github.com/mgarin/weblaf/

 WebTextField txtName = new com.alee.laf.text.WebTextField(); txtName.setHideInputPromptOnFocus(false); txtName.setInputPrompt("Name"); txtName.setInputPromptFont(new java.awt.Font("Ubuntu", 0, 18)); txtName.setInputPromptForeground(new java.awt.Color(102, 102, 102)); txtName.setInputPromptPosition(0); 

如果你仍然在寻找一个解决scheme,那么结合其他答案(Bart Kiers和culmat)供你参考:

 import javax.swing.*; import javax.swing.text.JTextComponent; import java.awt.*; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; public class HintTextField extends JTextField implements FocusListener { private String hint; public HintTextField () { this(""); } public HintTextField(final String hint) { setHint(hint); super.addFocusListener(this); } public void setHint(String hint) { this.hint = hint; setUI(new HintTextFieldUI(hint, true)); //setText(this.hint); } public void focusGained(FocusEvent e) { if(this.getText().length() == 0) { super.setText(""); } } public void focusLost(FocusEvent e) { if(this.getText().length() == 0) { setHint(hint); } } public String getText() { String typed = super.getText(); return typed.equals(hint)?"":typed; } } class HintTextFieldUI extends javax.swing.plaf.basic.BasicTextFieldUI implements FocusListener { private String hint; private boolean hideOnFocus; private Color color; public Color getColor() { return color; } public void setColor(Color color) { this.color = color; repaint(); } private void repaint() { if(getComponent() != null) { getComponent().repaint(); } } public boolean isHideOnFocus() { return hideOnFocus; } public void setHideOnFocus(boolean hideOnFocus) { this.hideOnFocus = hideOnFocus; repaint(); } public String getHint() { return hint; } public void setHint(String hint) { this.hint = hint; repaint(); } public HintTextFieldUI(String hint) { this(hint, false); } public HintTextFieldUI(String hint, boolean hideOnFocus) { this(hint, hideOnFocus, null); } public HintTextFieldUI(String hint, boolean hideOnFocus, Color color) { this.hint = hint; this.hideOnFocus = hideOnFocus; this.color = color; } protected void paintSafely(Graphics g) { super.paintSafely(g); JTextComponent comp = getComponent(); if(hint != null && comp.getText().length() == 0 && (!(hideOnFocus && comp.hasFocus()))) { if(color != null) { g.setColor(color); } else { g.setColor(Color.gray); } int padding = (comp.getHeight() - comp.getFont().getSize()) / 2; g.drawString(hint, 5, comp.getHeight() - padding - 1); } } public void focusGained(FocusEvent e) { if(hideOnFocus) repaint(); } public void focusLost(FocusEvent e) { if(hideOnFocus) repaint(); } protected void installListeners() { super.installListeners(); getComponent().addFocusListener(this); } protected void uninstallListeners() { super.uninstallListeners(); getComponent().removeFocusListener(this); } } Usage: HintTextField field = new HintTextField(); field.setHint("Here's a hint");