簡體   English   中英

如何在搖擺的JTextfield中設置像占位符一樣的文本

[英]How to set Text like Placeholder in JTextfield in swing

我想在加載表單時將一些文本放在文本字段中,以指示用戶,當用戶單擊該文本文件時,文本會自動刪除。

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

我已經編寫了上面的代碼,但是當用戶單擊我要刪除的文本按鈕時,它會顯示文本並保持原樣。

有什么辦法可以完成這項任務嗎?

我用來覆蓋文本字段的繪制方法,直到我最終得到更多的自定義文本字段,然后我才真正想要......

然后我發現這個提示 API使用簡單,不需要你擴展任何組件。 它還有一個不錯的“伙伴”API

這現在已包含在 SwingLabs、 SwingX 庫中,這使得它更易於使用......

例如(這里使用 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 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));  
        }  

    }  
});

你可以下載這個 NetBeans 插件,你可以用它來創建一個只有一行的占位符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM