Java 使用Thread.sleep()创建闪烁的JButton
我正在尝试使用JavaSwing制作游戏Simon。但是,我不知道如何更改JButton的背景色,等待几秒钟,然后将背景色更改回原始颜色。尽管一些人建议使用Swing定时器,但我不确定如何在现有代码中成功实现这些定时器 我试图简单地使用Thread.sleep()在更改颜色之前暂停2秒钟,但颜色似乎没有改变 这是我目前的代码:Java 使用Thread.sleep()创建闪烁的JButton,java,swing,jbutton,Java,Swing,Jbutton,我正在尝试使用JavaSwing制作游戏Simon。但是,我不知道如何更改JButton的背景色,等待几秒钟,然后将背景色更改回原始颜色。尽管一些人建议使用Swing定时器,但我不确定如何在现有代码中成功实现这些定时器 我试图简单地使用Thread.sleep()在更改颜色之前暂停2秒钟,但颜色似乎没有改变 这是我目前的代码: import java.util.*; import java.util.Random; import java.util.concurrent.TimeUnit; im
import java.util.*;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.border.*;
public class SimonGame extends JFrame{
private JButton begin;
Color dullRed = new Color(255, 128, 128);
Color dullYellow = new Color(255, 255, 128);
Color dullBlue = new Color(128, 128, 255);
Color dullGreen = new Color(128, 255, 128);
Color backgroundColor = new Color(1,1,1);
private JButton red;
private JButton yellow;
private JButton blue;
private JButton green;
// no-argument constructor
public SimonGame()
{
createUserInterface();
}
// create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
begin = new JButton();
begin.setBounds( 200,100,200,50 );
begin.setText("Begin Game");
contentPane.add( begin );
begin.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when submitJButton is pressed
public void actionPerformed( ActionEvent event )
{
try {
createPattern();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} // end anonymous inner class
); // end call to addActionListener
red = new JButton();
red.setBounds( 300,300,100,100 );
red.setBackground(dullRed);
red.setBorder(null);
contentPane.add( red );
red.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when submitJButton is pressed
public void actionPerformed( ActionEvent event )
{
//submitJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
yellow = new JButton();
yellow.setBounds( 300,200,100,100 );
yellow.setBackground(dullYellow);
yellow.setBorder(null);
contentPane.add( yellow );
yellow.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when submitJButton is pressed
public void actionPerformed( ActionEvent event )
{
//submitJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
green = new JButton();
green.setBounds( 200,300,100,100 );
green.setBackground(dullGreen);
green.setBorder(null);
contentPane.add( green );
green.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when submitJButton is pressed
public void actionPerformed( ActionEvent event )
{
//submitJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
blue = new JButton();
blue.setBounds( 200,200,100,100 );
blue.setBackground(dullBlue);
blue.setBorder(null);
contentPane.add( blue );
blue.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when submitJButton is pressed
public void actionPerformed( ActionEvent event )
{
//submitJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
setTitle( "Simon Game" ); // set title bar string
setSize( 600, 600 ); // set window size
setVisible( true );
}
private void setDefaultColors(){
red.setBackground(dullRed);
yellow.setBackground(dullYellow);
blue.setBackground(dullBlue);
green.setBackground(dullGreen);
}
private void createPattern() throws InterruptedException
{
Random rand = new Random();
int iter = 4;
int generatedPattern[] = new int[iter];
for(int i = 0; i<iter; i++)
{
int randomColor = rand.nextInt(4) +1;
generatedPattern[i] = randomColor;
switch(randomColor)
{
case 1:
red.setBackground(Color.RED);
Thread.sleep(2000);
red.setBackground(dullRed);
break;
case 2:
yellow.setBackground(Color.YELLOW);
Thread.sleep(2000);
yellow.setBackground(dullYellow);
break;
case 3:
blue.setBackground(Color.BLUE);
Thread.sleep(2000);
blue.setBackground(dullBlue);
break;
case 4:
green.setBackground(Color.GREEN);
Thread.sleep(2000);
green.setBackground(dullGreen);
break;
default:
System.out.print("The program is super broken");
break;
}
}
}
public static void main( String[] args )
{
SimonGame application = new SimonGame();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
} // end method main
}
import java.util.*;
导入java.util.Random;
导入java.util.concurrent.TimeUnit;
导入java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
导入javax.swing.Timer;
导入javax.swing.border.*;
公共类SimonGame扩展了JFrame{
私人按钮开始;
颜色暗淡=新颜色(255、128、128);
颜色暗黄色=新颜色(255、255、128);
颜色淡蓝色=新颜色(128、128、255);
颜色淡绿色=新颜色(128、255、128);
颜色背景颜色=新颜色(1,1,1);
私人朱布顿红;
私用钮扣黄;
私人杰布顿蓝;
私人杰布顿格林;
//无参数构造函数
公共SimonGame()
{
createUserInterface();
}
//创建并定位GUI组件;注册事件处理程序
私有void createUserInterface()
{
//获取用于附加GUI组件的内容窗格
容器contentPane=getContentPane();
//启用GUI组件的显式定位
contentPane.setLayout(null);
begin=newjbutton();
begin.setBounds(200100200,50);
begin.setText(“开始游戏”);
contentPane.add(开始);
begin.addActionListener(
新建ActionListener()//匿名内部类
{
//按下submitJButton时调用的事件处理程序
已执行的公共无效操作(操作事件)
{
试一试{
createPattern();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}//结束匿名内部类
);//结束对addActionListener的调用
红色=新的JButton();
红色。立根(300100100);
红色。后退地面(暗沉);
红色。订单号(空);
contentPane.add(红色);
red.addActionListener(
新建ActionListener()//匿名内部类
{
//按下submitJButton时调用的事件处理程序
已执行的公共无效操作(操作事件)
{
//submitJButtonActionPerformed(事件);
}
}//结束匿名内部类
);//结束对addActionListener的调用
黄色=新的JButton();
黄色。立根(300200100100);
黄色。退化背景(暗黄色);
黄色。订单号(空);
contentPane.add(黄色);
yellow.addActionListener(
新建ActionListener()//匿名内部类
{
//按下submitJButton时调用的事件处理程序
已执行的公共无效操作(操作事件)
{
//submitJButtonActionPerformed(事件);
}
}//结束匿名内部类
);//结束对addActionListener的调用
绿色=新的JButton();
绿色。挫折(200300100100);
绿色。退步地(暗绿色);
绿色。订单号(空);
contentPane.add(绿色);
green.addActionListener(
新建ActionListener()//匿名内部类
{
//按下submitJButton时调用的事件处理程序
已执行的公共无效操作(操作事件)
{
//submitJButtonActionPerformed(事件);
}
}//结束匿名内部类
);//结束对addActionListener的调用
蓝色=新的JButton();
蓝色。挫折(200100100);
蓝色。背景色(淡蓝色);
蓝色。订单号(空);
contentPane.add(蓝色);
blue.addActionListener(
新建ActionListener()//匿名内部类
{
//按下submitJButton时调用的事件处理程序
已执行的公共无效操作(操作事件)
{
//submitJButtonActionPerformed(事件);
}
}//结束匿名内部类
);//结束对addActionListener的调用
setTitle(“西蒙游戏”);//设置标题栏字符串
设置大小(600600);//设置窗口大小
setVisible(真);
}
私有void setDefaultColors(){
红色。后退地面(暗沉);
黄色。退化背景(暗黄色);
蓝色。背景色(淡蓝色);
绿色。退步地(暗绿色);
}
私有void createPattern()引发InterruptedException
{
Random rand=新的Random();
int-iter=4;
int-generatedPattern[]=新的int[iter];
对于(int i=0;我查看了“为什么”以及可能的“如何”和“尽管还有一些其他问题建议使用Swing计时器,但我不确定如何在我现有的代码中成功地实现它们。”1)尝试做一些事情&如果失败..2)返回给我们一个具体的问题。-Thread.sleep()
不是这种情况下的解决方法。”尽管还有一些其他问题提出了建议