我正在制作一个基于文本的游戏,用户输入文本来解决游戏。我决定使用 java swing 来显示文本,并且我希望将 textPane 的背景设置为黑色。我已经尝试了我发现的所有内容(已注释掉),但似乎没有一个有效。
private JTextPane blackJTextPane() {
//JTextPane area = new JTextPane();
//area.setBackground(Color.BLACK);
//area.setForeground(Color.WHITE);
JEditorPane area = new JEditorPane();
Color bgColor = Color.BLACK;
UIDefaults defaults = new UIDefaults();
defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
area.putClientProperty("Nimbus.Overrides", defaults);
area.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
area.setBackground(bgColor);
return area;
}
public Everything(){
super("Game");
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception exc) {
// ignore error
}
setSize(600,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BorderLayout layout = new BorderLayout();
setLayout(layout);
setVisible(true);
text = new JLabel("");
text.setText("Text:");
texts = new JTextField(20);
texts.setBackground(Color.white);
texts.setText("");
JPanel panel = new JPanel();
panel.add(text );
panel.add(texts);
texts.addActionListener(this);
add(panel, BorderLayout.SOUTH);
UIDefaults defs = UIManager.getDefaults();
defs.put("TextPane.background", new ColorUIResource(Color.BLACK));
defs.put("TextPane.inactiveBackground", new ColorUIResource(Color.BLACK));
area = blackJTextPane();
area.setEditable(false);
style = area.addStyle("style", null);
//StyleConstants.setBackground(style, Color.black);
JScrollPane pane = new JScrollPane(area);
add(pane, BorderLayout.CENTER);
doc = area.getStyledDocument();
button.addActionListener(this);
setVisible(true);
}
此处未显示导入内容,但当我运行带有任何注释掉的部分的游戏时,游戏中没有任何错误。
最佳答案
您可能会遇到 Nimbus 不遵守背景颜色设置的问题。试试这个来覆盖颜色:
JEditorPane area = new JEditorPane();
Color bgColor = Color.BLACK;
UIDefaults defaults = new UIDefaults();
defaults.put("EditorPane[Enabled].backgroundPainter", bgColor);
area.putClientProperty("Nimbus.Overrides", defaults);
area.putClientProperty("Nimbus.Overrides.InheritDefaults", true);
area.setBackground(bgColor);
关于java - JTextPane 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22674575/