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