【问题标题】:Hint for JTextField / JPasswordFieldJTextField / JPasswordField 的提示
【发布时间】:2015-02-26 18:37:56
【问题描述】:

我构建了一个扩展 JTextField 类的类和一个自己的提示函数。

package functions;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JTextField;

public class TextField extends JTextField {

private String hint;
private Color cForeground;
private Color cHint;

public void setHint(String s) {
    hint = s;
    cForeground = getForeground();

    setText(hint);
    cHint = new Color(cForeground.getRed(), cForeground.getGreen(),
            cForeground.getBlue(), cForeground.getAlpha() / 2);

    addFocusListener(new FocusListener() {

        @Override
        public void focusLost(FocusEvent arg0) {
            if (getText().equals("")) {
                setForeground(cHint);
                setText(hint);
            }
        }

        @Override
        public void focusGained(FocusEvent arg0) {
            if (getText().equals(hint)) {
                setText("");
                setForeground(cForeground);
            }
        }
    });
}
}

1) 目前,我的提示仅在没有集中注意力时才会出现。但我希望我的提示在它为空时是可见的——当它集中时也是如此。我玩过ActionListener 而不是FocusListener,但没听懂。

2) 我想为 JPasswordField 做同样的事情,但我不想在 2 个不同的类中编写相同的方法。有没有一种方法可以在两个类中指向同一个方法,而一个扩展 JTextField 另一个扩展 JPasswordField?

3) 我决定是否通过调用getText() 来显示提示,但这在处理密码时并不好(我不想因为记录密码而受到指责……)。有没有其他方法可以防止这种情况发生?

顺便说一句:我知道TextPrompt,但我想构建一个自己的简单解决方案。

【问题讨论】:

  • I know about TextPrompt, but I want to build an own simple solution - 然后从可行的东西开始,并根据您的要求对其进行定制。也许创建一个简单的解决方案并不容易。 But I want my hint to be visible when it's empty - also when it's focused.TextPrompt 这样做。 I want the same thing for JPasswordField, but I don't want to write the same method in 2 different classes 好吧,Text Prompt 再次向您展示了如何做到这一点,因此请从一个工作示例中学习。
  • 假设您在谈论这个Text Prompt 课程,我的cmets 就完成了。

标签: java swing listener jtextfield hint


【解决方案1】:

据我了解,您需要 HTML 中的占位符。然后像这样覆盖paintComponent 方法:

public class STextField extends JTextField{
    public static final Color placeholderColor = new Color(cForeground.getRed(), cForeground.getGreen(), cForeground.getBlue(), cForeground.getAlpha() / 2);
    public STextField(String placeholder){
        this.placeholder = placeholder;
    }
    protected void paintComponent(final Graphics pG) {
        super.paintComponent(pG);
        if(placeholder.length() == 0 || getText().length() > 0)
            return;
        final Graphics2D g = (Graphics2D) pG;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setColor(placeholderColor);
        int offset = 4; // This value depends on height of text field. Probably can be calculated from font size.
        g.drawString(placeholder, getInsets().left, pG.getFontMetrics().getMaxAscent() + offset);
    }
    private String placeholder;
}

【讨论】:

    猜你喜欢
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多