java - JTextPane 背景颜色

标签 java swing jtextpane

我正在制作一个基于文本的游戏,用户输入文本来解决游戏。我决定使用 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/

相关文章:

java - 将图像拖放到Jpanel中并将其放入应用程序目录中

java - 不同机器上的字体及其大小

java - 对话框在没有应用程序的情况下可见。在替代选项卡上

java - 从 JTextPane 中删除最后一个字符

java - 编译使用 geany\u0000 编写的程序时出现非法字符错误

java - 使用 Spring 和 Webapp 属性占位符查找属性

java - 以 HTML 形式查看 JTextPane 中的样式文本

java - 从 JTextPane 获取原始文本

java - JSP 将数组作为参数传递,将方括号添加到第一个和最后一个元素

java - 实现在执行过程中返回结果的多个并发线程的最佳方法?