繁体   English   中英

JFrame颜色变化

[英]JFrame color change

我有一个JFrame出现在计时器的末尾。 下面的代码弹出框架和声音。 从菜单中设置框架的颜色,然后将其提供给框架。 我需要框架在默认颜色和我在菜单中选择的颜色之间切换。 提前致谢

new Thread(new Runnable()
      {
        public void run()
        {
            JFrame frame= new JFrame(); 
    frame.setVisible(true);
            frame.setSize(600, 400);
            frame.setLocation(200, 200);
            frame.setTitle("ALARM");
            frame.getContentPane().setBackground(GUI.this.timerPanel.colorButton.getBackground());  // *This is the source for the color i select in the menu* 
            JLabel welcome = new JLabel("",SwingConstants.CENTER);
            welcome.setFont(new Font("Serif", Font.PLAIN, 48));
            welcome.setText("ALARM ALARM ALARM");
            frame.add(welcome);
            new SoundEngine().playSound();





        }
      })

        .start();

Swing不是Thread友好的,请尝试了解SwingUtility.InvokeLater

要更改Color使用JFrame.setBackGround(color)

现在,您将如何切换?

对我而言,最好的方法是创建一个名为UtilClass

public class Util{
     private static int ser=0;
     private static Color[] backColor=new Color[]{Color.red,Color.green,Color.white};
     public static void setBC(JFrame frame){
         frame.setBackGround(backColor[ser++%backColor.lenght]);
}

现在,在扩展的 JFrame类或部分上,只需调用Util.setBC(frame)

它在这三种颜色之间变化,您可以根据需要添加更多或什至删除soem。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM