如何使用 Nimbus 外观更改 JPanel 的背景颜色? [英] How to change the background color for JPanels with Nimbus Look and Feel?

查看:44
本文介绍了如何使用 Nimbus 外观更改 JPanel 的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为应用程序中的所有 JPanel 使用不同的背景颜色.使用 Nimbus 外观和感觉时如何做到这一点?

I want to use a different background color for all my JPanels in an application. How can I do that when using Nimbus Look and Feel?

我按照更改颜色主题更改颜色Nimbus Look and Feel 中的组件.

I follow Changing the Color Theme to change the color of components in Nimbus Look and Feel.

它只是有时有效,随机.如果我在更改颜色之前设置了 PropertyChagneListener,则只会通知一次.

It only works sometimes, randomly. If I set a PropertyChagneListener before I change the color, it is only notified once.

这是一些测试代码:

public class RedPanels extends JFrame {

  public RedPanels() {
    JPanel panel = new JPanel();
    add(panel);
    setPreferredSize(new Dimension(100, 100));
    pack();
    setVisible(true);
  }

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

      @Override
      public void run() {

        try {
          for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                UIManager.getDefaults().addPropertyChangeListener(
                                               new PropertyChangeListener() {

                  @Override
                  public void propertyChange(PropertyChangeEvent event) {
                    if (event.getPropertyName().equals("Panel.background")) {
                      System.out.println("color changed");
                    }

                });
                UIManager.put("Panel.background", new Color(255,0,0));
                break;
            }
          }
        } catch (Exception e) {
            // Nimbus is not available.
        }
        new RedPanels();
        }
    });
  }
}

推荐答案

UIManager.getLookAndFeelDefaults().put("Panel.background", Color.RED);

这篇关于如何使用 Nimbus 外观更改 JPanel 的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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