使用Thread.sleep()

时间:2017-06-01 21:15:04

标签: java swing jbutton

我正在尝试使用Java Swing制作游戏Simon。但是,我无法弄清楚如何更改JButton的背景颜色,等待几秒钟,然后将背景颜色更改回原始颜色。虽然有几个other questions建议使用Swing Timers,但我不确定如何在现有代码中成功实现这些。

我试图简单地使用Thread.sleep()以便在更改颜色之前暂停2秒,但颜色似乎没有改变。

这是我目前的代码:

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

}

我非常感谢任何协助。

谢谢。

0 个答案:

没有答案