Java 如何在Swing中的JTextfield中设置类似占位符的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22396282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:30:42  来源:igfitidea点击:

How to set Text like Placeholder in JTextfield in swing

javaswingjtextfieldpromptwindowbuilder

提问by Uday A. Navapara

I want to put some texts in text-Field when the form is load which instruct to user and when user click on that text-filed the texts remove automatically.

我想在加载表单时在 text-Field 中放置一些文本,以指示用户,当用户单击该文本文件时,文本会自动删除。

 txtEmailId = new JTextField();
 txtEmailId.setText("Email ID");

i have wrote above code but it display the text and keep as it is when user click on that text button i want to remove it.

我已经写了上面的代码,但它显示文本并在用户单击该文本按钮时保持原样,我想将其删除。

is there any way to do this task?

有什么办法可以完成这项任务吗?

采纳答案by MadProgrammer

I use to override the text fields paint method, until I ended up with more custom text fields then I really wanted...

我用来覆盖文本字段的绘制方法,直到我最终得到了更多的自定义文本字段,然后我才真正想要......

Then I found this prompt APIwhich is simple to use and doesn't require you to extend any components. It also has a nice "buddy" API

然后我发现了这个提示 API,它使用简单,不需要你扩展任何组件。它还有一个不错的“伙伴”API

This has now been included in the SwingLabs, SwingX librarywhich makes it even eaiser to use...

现在,它已包含在 SwingLabs、SwingX 库中,这使得它更易于使用...

For example (this uses SwingX-1.6.4)

例如(这里使用 SwingX-1.6.4)

PromptSupport

即时支持

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.prompt.PromptSupport;

public class PromptExample {

    public static void main(String[] args) {
        new PromptExample();
    }

    public PromptExample() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JTextField bunnies = new JTextField(10);
                JTextField ponnies = new JTextField(10);
                JTextField unicorns = new JTextField(10);
                JTextField fairies = new JTextField(10);

                PromptSupport.setPrompt("Bunnies", bunnies);
                PromptSupport.setPrompt("Ponnies", ponnies);
                PromptSupport.setPrompt("Unicorns", unicorns);
                PromptSupport.setPrompt("Fairies", fairies);

                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIDE_PROMPT, bunnies);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.HIGHLIGHT_PROMPT, ponnies);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, unicorns);

                PromptSupport.setFontStyle(Font.BOLD, bunnies);
                PromptSupport.setFontStyle(Font.ITALIC, ponnies);
                PromptSupport.setFontStyle(Font.ITALIC | Font.BOLD, unicorns);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                frame.add(bunnies, gbc);
                frame.add(ponnies, gbc);
                frame.add(unicorns, gbc);
                frame.add(fairies, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

回答by miguel angel

JTextField busqueda = new JTextField(20);  

add(busqueda);   
busqueda.setHorizontalAlignment(SwingConstants.CENTER);  

if (busqueda.getText().length() == 0) {  
    busqueda.setText("Buscar");  
    busqueda.setForeground(new Color(150, 150, 150));  
}  

busqueda.addFocusListener(new FocusListener() {  

    @Override  
    public void focusGained(FocusEvent e) {  
        busqueda.setText("");  
        busqueda.setForeground(new Color(50, 50, 50));  
    }  

    @Override  
    public void focusLost(FocusEvent e) { 

        if (busqueda.getText().length() == 0) {  
            busqueda.setText("Buscar");  
            busqueda.setForeground(new Color(150, 150, 150));  
        }  

    }  
});

回答by Uzziel Contreras

You can download this NetBeans pluginwhich you can use to create a placeholder with just one line.

您可以下载这个 NetBeans 插件,您可以使用它来创建仅一行的占位符。