提示JTextField使其显示为灰色

时间:2014-09-03 02:13:56

标签: java swing prompt swingx digit

我正在使用JTextFields的

只有一个问题:

Prompt

正如你所看到的,背景是灰色阴影,我甚至试图将背景颜色设置为白色,它仍然是灰色的。下面是我当前设置提示的代码:

final JTextField aText = new JTextField(6);
final JTextField bText = new JTextField(6);  
PromptSupport.setPrompt("Digit", aText);

这是对的吗?谢谢。

1 个答案:

答案 0 :(得分:0)

这似乎是PromptSupport和MaxOS X之间的错误(或者至少是我遇到问题的地方)

我尝试使用PromprtSupport.setBackground,但似乎没有用,所以我最终使用了PromptSupport.setBackgroundPainter

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import org.jdesktop.swingx.painter.Painter;
import org.jdesktop.swingx.prompt.PromptSupport;

public class TestPrompt {

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

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

                JTextField prompt = new JTextField(20);
                PromptSupport.setPrompt("Go ahead, make my day", prompt);
                PromptSupport.setFocusBehavior(PromptSupport.FocusBehavior.SHOW_PROMPT, prompt);
                PromptSupport.setBackgroundPainter(new Painter() {
                    @Override
                    public void paint(Graphics2D g, Object object, int width, int height) {
                        g.setColor(UIManager.getColor("TextField.background"));
                        g.fillRect(0, 0, width, height);
                    }
                }, prompt);

                prompt.setOpaque(true);
                JTextField noPrompt = new JTextField(20);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridwidth = GridBagConstraints.REMAINDER;
                gbc.insets = new Insets(4, 4, 4, 4);
                frame.add(prompt, gbc);
                frame.add(noPrompt, gbc);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

这是一个解决方案,而不是解决方案,因为仍然存在一些问题......