无法动态更新外观

时间:2015-01-13 22:54:35

标签: java swing look-and-feel

我有一个程序,我希望能够在“白天”和“夜晚”模式之间切换,“夜间”模式在所有内容上都有50%的灰色背景。我这样做是通过从这个页面调用所有“背景”键的UIManager.put(键,新颜色)来实现的:

http://alvinalexander.com/java/java-uimanager-color-keys-list

然后我将SwingUtilities.updateComponentTreeUI()与java.awt.Window.getWindows()一起使用,以使现有窗口获取更改。

当我切换模式时,新创建的窗口将具有适当的背景,因此第一部分可以工作。但是,现有的窗口表现得很奇怪:它们暂时闪烁到新颜色,然后切换回来。例如,如果我在Day模式下启动程序,那么主程序窗口实际上停留在Day模式,反之亦然。

我试图制作一个示例程序。它的行为与我现有的程序完全不同,但它仍然表现得很奇怪:外观只能被修改一次。之后,将忽略新的更改:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Frame;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


public class Demo {
   public static void main(String[] args) {
      new Demo();
   }

   public Demo() {
      JFrame frame = new JFrame("Look and feel test");
      frame.setLayout(new GridLayout(3, 3));
      // Fill in non-central locations in the layout.
      // Hacky way to keep the button from monopolizing the frame
      for (int i = 0; i < 4; ++i) {
         frame.add(new JLabel(""));
      }
      JButton button = new JButton("Set new LAF");
      button.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            setNewLAF();
         }
      });
      frame.add(button);
      // Fill in non-central locations in the layout.
      for (int i = 0; i < 4; ++i) {
         frame.add(new JLabel(""));
      }
      frame.setMinimumSize(new Dimension(400, 400));
      frame.show();
      System.out.println("Initial frame background is " + frame.getBackground());
   }

   public void setNewLAF() {
      Random rng = new Random();
      Color newBackground = new Color(rng.nextInt(255),
            rng.nextInt(255), rng.nextInt(255));
      System.out.println("New color is " + newBackground);
      UIManager.put("Panel.background", newBackground);
      for (Frame f : Frame.getFrames()) {
         SwingUtilities.updateComponentTreeUI(f);
         System.out.println("Frame now has background " + f.getBackground());     
      }
   }
}

这会产生如下输出:

Initial frame background is com.apple.laf.AquaImageFactory$SystemColorProxy[r=238,g=238,b=238]
New color is java.awt.Color[r=243,g=209,b=134]
Frame now has background java.awt.Color[r=243,g=209,b=134]
New color is java.awt.Color[r=205,g=141,b=58]
Frame now has background java.awt.Color[r=243,g=209,b=134]
New color is java.awt.Color[r=141,g=22,b=92]
Frame now has background java.awt.Color[r=243,g=209,b=134]

感谢您提出的任何建议。

2 个答案:

答案 0 :(得分:1)

  

它仍然表现得很奇怪:外观只能修改一次。

Color newBackground = new Color(rng.nextInt(255),rng.nextInt(255), rng.nextInt(255));

您需要使用:

Color newBackground = new ColorUIResource(rng.nextInt(255),rng.nextInt(255), rng.nextInt(255));

updateComponentTreeUI()仅替换属于UI的资源。您可以将Color包裹在ColorUIResource

答案 1 :(得分:0)

为什么不用这样的代码替换基于UIManager的代码?

public void setNewLAF(JFrame frame) {
    Random rng = new Random();
    Color newBackground = new Color(rng.nextInt(255), rng.nextInt(255),
            rng.nextInt(255));
    System.out.println("New color is " + newBackground);
    frame.setBackground(newBackground);
    frame.getContentPane().setBackground(newBackground);
}

它可能不适合你的情况(例如,如果你有很多帧),但我认为你可以将你的帧转储到ArrayList<JFrame>并迭代它们。

另外,只是为了通知您:show()已弃用。使用setVisible(true)

相关问题