java swing JTextField设置PlaceHolder

java swing JTextField设置PlaceHolder,第1张

java swing JTextField设置PlaceHolder

试试这个课程

package playground;import java.awt.*;import javax.swing.*;import javax.swing.text.document;@SuppressWarnings("serial")public class PlaceholderTextField extends JTextField {    public static void main(final String[] args) {        final PlaceholderTextField tf = new PlaceholderTextField("");        tf.setColumns(20);        tf.setPlaceholder("All your base are belong to us!");        final Font f = tf.getFont();        tf.setFont(new Font(f.getName(), f.getStyle(), 30));        JOptionPane.showMessageDialog(null, tf);    }    private String placeholder;    public PlaceholderTextField() {    }    public PlaceholderTextField(        final document pDoc,        final String pText,        final int pColumns)    {        super(pDoc, pText, pColumns);    }    public PlaceholderTextField(final int pColumns) {        super(pColumns);    }    public PlaceholderTextField(final String pText) {        super(pText);    }    public PlaceholderTextField(final String pText, final int pColumns) {        super(pText, pColumns);    }    public String getPlaceholder() {        return placeholder;    }    @Override    protected void paintComponent(final Graphics pG) {        super.paintComponent(pG);        if (placeholder == null || placeholder.length() == 0 || getText().length() > 0) { return;        }        final Graphics2D g = (Graphics2D) pG;        g.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);        g.setColor(getDisabledTextColor());        g.drawString(placeholder, getInsets().left, pG.getFontMetrics() .getMaxAscent() + getInsets().top);    }    public void setPlaceholder(final String s) {        placeholder = s;    }}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存