如何在JTextfield中设置像Placeholder一样的文本 [英] How to set Text like Placeholder in JTextfield in swing

查看:188
本文介绍了如何在JTextfield中设置像Placeholder一样的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在表单加载时将一些文本放在text-Field中,这些文本指示给用户,当用户点击该文本时,文本会自动删除。

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.

 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?

推荐答案

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

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

然后我发现此提示API 易于使用,不需要您扩展任何组件。它还有一个很好的伙伴API

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

这已经包含在SwingLabs中, SwingX库,这使得它甚至可以使用......

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

例如(这使用SwingX-1.6.4)

For example (this uses SwingX-1.6.4)

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

}

这篇关于如何在JTextfield中设置像Placeholder一样的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆