我正在制作一个基于文本的游戏,用户输入文字来解决该游戏。我决定使用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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句