重击鼹鼠游戏

时间:2016-07-09 03:55:25

标签: java

我是java的新手,我正在使用JFrame与JButton进行一场傻瓜游戏。目前,我有一个5x5网格的按钮,这是我得到的。我有3个按钮是X(表示痣),22个是O(表示空洞)。我希望按钮值可以随机播放,以便每2秒对值进行随机化。我该怎么做呢?很抱歉这是一个新手,几个星期前我几乎开始java和JFrames仍然困惑我哈哈。这是我目前的代码,谢谢;

import javax.swing.*;
import java.awt.*;
public class Whack_A_Mole extends JFrame {
JButton[][] square = new JButton[5][5];
JButton button1, button2;
static JLabel label = new JLabel();


Whack_A_Mole() {
 super("Whack a Mole");
    JPanel p = new JPanel(new GridLayout(5,5));
    for(int i = 0; i < 5; i++) {
        for(int j = 0; j < 5; j++) {
            square[i][j] = new JButton();
            p.add(square[i][j]);
        }
    }    
    add(p, BorderLayout.CENTER);      
    p = new JPanel(new GridLayout(1,2));              
    setSize(600, 600);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setVisible(true);        
    setLocationRelativeTo(null);

}

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

}

2 个答案:

答案 0 :(得分:1)

如果将对象放在ArrayList中,则可以使用shuffle()方法对其顺序进行洗牌。至于时序,要么使用Thread.sleep(millisecondsAmt),要么使用Timer。我更喜欢util.Timer,特别是当动作无限重复或重复多次时。

答案 1 :(得分:0)

创建一个线程,每隔2秒将更新功能添加到队列中。

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowEvent;
import java.util.Random;

public class Wack_A_Mole extends JFrame {
    JButton[][] square = new JButton[5][5];
    JButton button1, button2;
    static JLabel label = new JLabel();
    private Thread updateWoker;

    Whack_A_Mole() {

     super("Whack a Mole");
        JPanel p = new JPanel(new GridLayout(5,5));
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5; j++) {
                square[i][j] = new JButton();
                p.add(square[i][j]);
            }
        }    
        add(p, BorderLayout.CENTER);      
        p = new JPanel(new GridLayout(1,2));              
        setSize(600, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setVisible(true);        
        setLocationRelativeTo(null);


    }
     void start(){
           updateWoker=new Thread(new Runnable(){
                            public void run(){
                                Runnable r=new Runnable(){                
                                    public void run() {
                                            buttonUpdate();  // call buttonUpdate every two seconds
                                    }
                                };
                                while (true){
                                        javax.swing.SwingUtilities.invokeLater(r);
                                        try{Thread.sleep(2000);} catch (InterruptedException ex) {  
                                        return;
                                    }
                                }   
                            }
                    }
            );
           updateWoker.start();     
     }
    public void buttonUpdate(){ // random update can be done in this function
        Random r=new Random();
            for(int i=0;i<square.length;i++){
                    for(int j=0;j<square[i].length;j++){
                        if(r.nextInt() %2==0)
                            square[i][j].setText("O");
                        else
                            square[i][j].setText("X");                         
                    }
            }
    }
    public void processWindowEvent(WindowEvent e) { 
        if (e.getID() == WindowEvent.WINDOW_CLOSING) { // making sure to stop the thread after gui closes
            if(updateWoker.isAlive()){
                updateWoker.interrupt();
            }
            dispose();
        }
    }
    public static void main(String[] args) throws InterruptedException {
            final Whack_A_Mole theTest=new Whack_A_Mole();
            theTest.start();
    }

}