Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 灵光L&;F尝试使用UIManager更改JFormattedTextField的背景色 SSCCE 问题_Java_Swing_Look And Feel_Nimbus_Uimanager - Fatal编程技术网

Java 灵光L&;F尝试使用UIManager更改JFormattedTextField的背景色 SSCCE 问题

Java 灵光L&;F尝试使用UIManager更改JFormattedTextField的背景色 SSCCE 问题,java,swing,look-and-feel,nimbus,uimanager,Java,Swing,Look And Feel,Nimbus,Uimanager,简单地说,我正在尝试将启用的的JFormattedTextField的背景色从默认白色更改为RGB颜色。我使用找到的表(此处的链接)来查找适当的名称 我意识到这个博客有点过时(6年了),而且从那时起,Nimbus已经更新了很多次,所以这可能是我的问题 如何使用UIManager更改JFormattedTextField的背景色 修正码 我更新了上面的代码,现在应该可以正常工作了。问题是使用了ColorUIResource,而不仅仅是Color 如何使用UIManager来更改背景颜色 JForm

简单地说,我正在尝试将启用的
JFormattedTextField
的背景色从默认白色更改为RGB颜色。我使用找到的表(此处的链接)来查找适当的名称

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

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

修正码 我更新了上面的代码,现在应该可以正常工作了。问题是使用了
ColorUIResource
,而不仅仅是
Color

如何使用UIManager来更改背景颜色 JFormattedTextField

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

像这样的

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

    }
}

你查过链接了吗
FormattedTextField.background
我很愚蠢,我没有把它包括在SSCCE中。再次更新我的代码。但又短又甜,不管用。编辑草稿,UIResource显然不是设置它的方法。@Eric实际上,如果您阅读了我提供的链接,如果您设置了
FormattedTextField[Enabled]。backgroundPainter
或您希望此属性为
Painter
 UIManager.put("FormattedTextField.background", Color.RED);