JTextPane背景色 [英] JTextPane Background Color

查看:179
本文介绍了JTextPane背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个基于文本的游戏,用户输入文字来解决该游戏.我决定使用java swing来显示文本,并且我想让textPane的背景为黑色.我已经尝试了所有发现(注释掉)的东西,但是似乎都没有用.

I am making a text-based game where the user inputs text to solve the game. I decided to use java swing to display the text, and I want to have the background of the textPane to be black. I have tried everything I have found (commented out), but non of it seems to work.

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







}

这里没有显示导入的图片,但是当我使用那些已注释掉的零件运行游戏时,游戏中没有错误.

The imports are not pictured here but there are no errors in the game when I run it with any of those commented out parts.

推荐答案

您可能会遇到Nimbus不遵守背景颜色设置的问题.尝试使用此方法覆盖颜色:

You may have an issue with Nimbus not respecting the background color settings. Try this to override the color:

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

这篇关于JTextPane背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆