如何更改JPanels的背景颜色与Nimbus看和感觉? [英] How to change the background color for JPanels with Nimbus Look and Feel?

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

问题描述

我想在应用程序中为我的所有JPanels使用不同的背景颜色。在使用Nimbus Look and Feel时,我该怎么做?



我按照更改颜色主题以更改Nimbus外观中的组件颜色。



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



这里是一些测试代码:

  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不可用。
}
new RedPanels();
}
});
}
}


解决方案

p> UIManager.getLookAndFeelDefaults()。put(Panel.background,Color.RED);


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?

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

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

Here is some test code:

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

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

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