Java JTextPane背景色

Java JTextPane背景色,java,swing,jtextpane,Java,Swing,Jtextpane,我正在制作一个基于文本的游戏,用户输入文本来解决游戏。我决定使用JavaSwing来显示文本,我希望文本窗格的背景为黑色。我已经尝试了所有我发现的东西(注释掉了),但是没有一个看起来有效 private JTextPane blackJTextPane() { //JTextPane area = new JTextPane(); //area.setBackground(Color.BLACK); //area.setForeground(Color.WHITE

我正在制作一个基于文本的游戏,用户输入文本来解决游戏。我决定使用JavaSwing来显示文本,我希望文本窗格的背景为黑色。我已经尝试了所有我发现的东西(注释掉了),但是没有一个看起来有效

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

@Slushy_G将其放在
BLACK jTextPane()
中,而不是
区域.setBackground(Color.BLACK)给出错误,我将更新代码以显示新的version@Slushy_G它应该是
area
而不是
area
。是的,但是如果我将area改为JEditorPane,它会在return语句中出错,当我尝试使用Style和getStyledDocument()@Slushy\G时,我没有注意到您正在使用
JTextPane
。将init更改为
JTextPane
,并使用
defaults.put(“TextPane[Enabled].backgroundPainter”,bgColor)