[英]JFrame color change
I have a JFrame that appears at the end of a timer. 我有一个JFrame出现在计时器的末尾。 The code below pops up the frame and a sound.
下面的代码弹出框架和声音。 The color of the frame is set from a menu and then it's given to the frame.
从菜单中设置框架的颜色,然后将其提供给框架。 I need the frame to alternate between default color and the color i select in the menu.
我需要框架在默认颜色和我在菜单中选择的颜色之间切换。 Thanks in advance
提前致谢
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 isn't Thread
friendly, try learning about SwingUtility.InvokeLater
. Swing不是
Thread
友好的,请尝试了解SwingUtility.InvokeLater
。
To change Color
use JFrame.setBackGround(color)
. 要更改
Color
使用JFrame.setBackGround(color)
。
Now how will you toggle? 现在,您将如何切换?
For me the best way is to create a Class
, named Util
. 对我而言,最好的方法是创建一个名为
Util
的Class
。
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]);
}
Now on your extended JFrame
class or section just call Util.setBC(frame)
. 现在,在扩展的
JFrame
类或部分上,只需调用Util.setBC(frame)
。
It changes between these three color, you can add more or even remove soem as you wish. 它在这三种颜色之间变化,您可以根据需要添加更多或什至删除soem。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.