java swing JTextField设置PlaceHolder

Ren*_*unt 19 java swing placeholder jtextfield

我创建了一个JTextField,现在我想在JTextField上设置占位符我不知道如何在JTextField上设置占位符?请帮助如何在JTextField上设置占位符文本

JTextField database=new JTextField("Enter Data Base Name");
database.setPreferredSize(database.getPreferredSize());
database.setText("");
Run Code Online (Sandbox Code Playgroud)

这是我的代码到文本字段现在在该代码中我想设置占位符如何在该JTextField上设置占位符

Pet*_*eng 26

试试这堂课:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 13

JTextField searchText;
Run Code Online (Sandbox Code Playgroud)

...

在构造函数中:

searchText = new JTextField("Search");
searchText.setForeground(Color.GRAY);
searchText.addFocusListener(new FocusListener() {
    @Override
    public void focusGained(FocusEvent e) {
        if (searchText.getText().equals("Search")) {
            searchText.setText("");
            searchText.setForeground(Color.BLACK);
        }
    }
    @Override
    public void focusLost(FocusEvent e) {
        if (searchText.getText().isEmpty()) {
            searchText.setForeground(Color.GRAY);
            searchText.setText("Search");
        }
    }
    });
Run Code Online (Sandbox Code Playgroud)

  • 欢迎来到堆栈溢出!通过提供一些与代码相关的上下文,这个答案会变得更加有用。 (3认同)

cam*_*ckr 9

如果我理解这个问题,文本提示会显示一种方法可以做到这一点.


Mah*_*med 8

这么简单的使用swingx jar就像这样

JTextArea txtMSISDNList = new JTextArea();  
PromptSupport.setPrompt("01197585960,01197585961", txtMSISDNList);
Run Code Online (Sandbox Code Playgroud)

  • 在此处下载jar文件:http://www.java2s.com/Code/Jar/s/Downloadswingx161jar.htm (2认同)
  • 不幸的是 swingx 不再处于开发阶段,因此使用时需要您自担风险(不确定它是否与 Java 8 兼容)。重要提示:无论 java2s.com 看起来多么值得信赖,永远不要从原始主页以外的任何地方下载 jar 文件(否则你永远不知道里面有什么!)!你仍然可以通过 [maven](https://maven.java.net/#nexus-search;gav~org.swinglabs.swingx~swingx-all~~~~kw,versionexpand) 获取 jar (参见“下载”右侧的列)。 (2认同)