【问题标题】:JTextArea background issueJTextArea 背景问题
【发布时间】:2012-09-15 16:10:17
【问题描述】:
private JDialog dialog;
private JTextArea text;
private JPanel buttons, filler;
private JRadioButton questions, list;
private ButtonGroup group;
private JButton confirm;

dialog = new JDialog(Main.masterWindow, lang.getString("newTitle"), true);
dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
dialog.setResizable(false);

text = new JTextArea();

//this works
text.setBackground(Color.RED);

//this both don't
text.setBackground((Color)UIManager.get("control");
text.setBackground(dialog.getContentPane().getBackground());

dialog.setVisible(true);

我正在使用 Nimbus L&F,“控制”是我的对话框的背景颜色。如果我设置任何其他颜色(在此示例中为红色),它会显示,但如果我将其设置为这个颜色,它会保持白色。

我在默认(金属)L&F 上没有问题...

有什么问题?

【问题讨论】:

    标签: java swing look-and-feel nimbus


    【解决方案1】:

    由于某种原因,它似乎不喜欢从UIManager.get 调用返回的ColorUIResource 对象。我不明白为什么,因为它源自Color

    如果你这样做

    JDialog dialog = new JDialog((JFrame) null, "Help", true);
    dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(), BoxLayout.Y_AXIS));
    
    JTextArea text = new JTextArea(10, 10);
    
    Color color = new Color(UIManager.getColor("control").getRGB()); // <-- Create a new color
    
    text.setBackground(bg);
    
    dialog.add(text);
    dialog.pack();
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);
    

    这似乎行得通。

    如果你必须这样做。我不这么认为,但我尝试的其他所有方法都不起作用

    【讨论】:

      【解决方案2】:

      尝试运行以下代码:

          System.out.println((Color)UIManager.get("control"));
      

      这将打印出您从 UIManager 获得的确切颜色。也许它实际上应该是白色的。告诉我打印的是什么

      编辑:

      //this both don't
      //text.setBackground(dialog.getContentPane.getBackground());
      

      首先,getContentPane 之后没有 (),即使它是一个方法。尝试这样做:text.setBackground(dialog.getContentPane().getBackground());

      【讨论】:

      • javax.swing.plaf.ColorUIResource[r=214,g=217,b=223]
      • 好的,我已经通过使用那些 RGB 值创建新颜色来修复它,但我不明白为什么我不能从对话框背景中获取颜色?在这种情况下没关系,但我的对话框背景可能会有所不同,如果我无法直接访问颜色,我希望 JTextArea 跟上。
      • 不,我只是在这里犯了一个错误,如果我在Eclipse中这样做会返回编译错误......
      • @Alex Coleman Nimbus DefaultsHow To
      猜你喜欢
      • 2015-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      相关资源
      最近更新 更多