首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nimbus L&F试图用UIManager改变UIManager的背景色

Nimbus L&F试图用UIManager改变UIManager的背景色
EN

Stack Overflow用户
提问于 2014-04-02 03:11:49
回答 1查看 571关注 0票数 1

SSCCE

代码语言:javascript
复制
import java.awt.EventQueue;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ColorUIResource;
import javax.swing.text.Document;
import javax.swing.text.NumberFormatter;

public class Test extends JFrame{

    private JFormattedTextField input, input2;
    private NumberFormatter formatter;


    public Test() {
        formatter = new NumberFormatter(NumberFormat.getNumberInstance());
        input = new JFormattedTextField(formatter);
        input2 = new JFormattedTextField(formatter);


        input.setColumns(4);
        input2.setColumns(4);
        input.setValue(0.0);
        JPanel panel = new JPanel();
        panel.add(input);
        panel.add(input2);

        add(panel);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    UIManager.put("nimbusBase", new ColorUIResource(0, 0, 0));
                    UIManager.put("FormattedTextField.background", Color.RED);
                    UIManager.put("control", new ColorUIResource(153, 76, 0));
                    UIManager.put("textForeground", new ColorUIResource(255, 153, 51));
                    break;
                } }
        } 
        catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Test();
            }
        });

    }
}

问题

简单地说,我试图将Enabled JFormattedTextField的背景色从默认的白色更改为RGB颜色。我使用查找这里的表(此博客中的链接)来查找合适的名称。

我意识到博客有点过时(6年),Nimbus从那时起已经更新了很多,所以这可能是我的问题。

如何使用UIManager更改JFormattedTextField的背景色?

修正码

我更新了上面的代码,它现在正常工作。问题是使用ColorUIResource而不是只使用Color

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-02 03:22:48

如何使用UIManager更改JFormattedTextField的背景色?

您必须设置此属性FormattedTextField.background,如此处所示Nimbus缺省值

就像这样

代码语言:javascript
复制
 UIManager.put("FormattedTextField.background", Color.RED);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22800817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档