我的Connect 4游戏中的Java线程

时间:2014-11-23 23:13:03

标签: java multithreading swing

好的,所以我正在为我的Java课程开发Connect 4游戏,这就是我到目前为止所拥有的。我正在尝试让睡眠线程方法工作(在底部),但我不知道如何让它只为第一次点击调用第一个,第二次点击时调用第二个,依此类推。

感谢任何帮助或意见

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.awt.Font;

import javax.swing.*;

public class ConnectMain  extends JFrame implements ActionListener {
private JButton btn1, btn2, btn3, btn4, btn5, btn6, btn7;
private JButton Reset;
private JTextField[][] text;
private char myLabel;
private Moving mover;

public ConnectMain() {

    this.setLayout(null);
    this.setBounds(400,300, 600, 600);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.getContentPane().setBackground(Color.cyan);

    text = new JTextField [7][6]; 
    for (int a = 0 ; a < 7; a++) 
    {

        for (int b = 0; b < 6; b++) 
        {

            text[a][b] = new JTextField();
            add(text[a][b]);
            text[a][b].setBounds(50 + (a*70), (b+3) * 50 , 50, 50);
            text[a][b].addActionListener(this);

            Font font = new Font("Verdana", Font.BOLD, 20);
            text[a][b].setFont(font);
            text[a][b].setHorizontalAlignmen(JFormattedTextField.CENTER);

            text[a][b].setForeground(Color.red); 



        }


    }       

        mover = new Moving(text);

        btn1 = new JButton();
        add(btn1);
        btn1.setBounds(50, 50, 50, 50);
        btn1.addActionListener(this);


        btn2 = new JButton();
        add(btn2);
        btn2.setBounds(120, 50, 50, 50);
        btn2.addActionListener(this);


        btn3 = new JButton();
        add(btn3);
        btn3.setBounds(190, 50, 50, 50);
        btn3.addActionListener(this);

        btn4 = new JButton();
        add(btn4);
        btn4.setBounds(260, 50, 50, 50);
        btn4.addActionListener(this);

        btn5 = new JButton();
        add(btn5);
        btn5.setBounds(330, 50, 50, 50);
        btn5.addActionListener(this);

        btn6 = new JButton();
        add(btn6);
        btn6.setBounds(400, 50, 50, 50);
        btn6.addActionListener(this);

        btn7 = new JButton();
        add(btn7);
        btn7.setBounds(470, 50, 50, 50);
        btn7.addActionListener(this);


        Reset = new JButton("Reset");
        add(Reset);
        Reset.setBounds(200, 470, 150, 50);
        Reset.addActionListener(this);


}
public static void main(String[] args) {
    new ConnectMain();
}
int space1 = 5;
int space2 = 5;
int space3 = 5;
int space4 = 5;
int space5 = 5;
int space6 = 5;
int space7 = 5;

@Override
public void actionPerformed(ActionEvent e){

    if (myLabel == 'X') { 
                    myLabel = 'O'; 
                } 
                else { 
                    myLabel = 'X'; 
                } 



        if (e.getSource() == btn1)
        {
            mover.start();
            text[0][space1].setText("" + myLabel);

            space1--;



    }
        if (space1 == -1)
        {
            btn1.setEnabled(false);


        }
    {
     if (e.getSource() == btn2)     
    {
            text[1][space2].setText("" + myLabel);

            space2--;

        } 
        if (space2 == -1)
        {
            btn2.setEnabled(false);
        }

     if (e.getSource() == btn3)     
    {
            text[2][space3].setText("" + myLabel);

            space3--;

        } 
        if (space3 == -1)
        {
            btn3.setEnabled(false);
        }

     if (e.getSource() == btn4)     
    {
            text[3][space4].setText("" + myLabel);

            space4--;

        } 
        if (space4 == -1)
        {
            btn4.setEnabled(false);
        }

    if (e.getSource() == btn5)      
    {
            text[4][space5].setText("" + myLabel);

            space5--;

        } 
    if (space5 == -1)
    {
        btn5.setEnabled(false);
    }

    if (e.getSource() == btn6)      
    {
            text[5][space6].setText("" + myLabel);

            space6--;

        } 
    if (space6 == -1)
    {
        btn6.setEnabled(false);
    }

    if (e.getSource() == btn7)      
    {
            text[6][space7].setText("" + myLabel);

            space7--;



        } 
    if (space7 == -1)
            {
                btn7.setEnabled(false);
            }

}
       if (e.getSource() == Reset)  
       {
           this.dispose();
           new ConnectMain();
       }

}


public class Moving extends Thread {
    JTextField text[][];
    public Moving(JTextField text[][])
    {
        this.text = text;
    }

    public void run () {
        for (int y = 0; y < 5; y++) {
            text[0][y].setText("" + myLabel);
            try {
                Thread.sleep(500);
            }
            catch (Exception e) {
                e.printStackTrace();

            }   
            text[0][y].setText("");



        }


        for (int y=0; y < 4; y++) {
            text[0][y].setText("" + myLabel);
            try {
                Thread.sleep(500);
            }
            catch (Exception e) {
                e.printStackTrace();

            }   
            text[0][y].setText("");


            /*
        }
        for (int y=0; y < 3; y++) {
            text[0][y].setText("" + myLabel);
            try {
                Thread.sleep(500);
            }
            catch (Exception e) {
                e.printStackTrace();

            }   
            text[0][y].setText("");


        }
        for (int y = 0; y < 2; y++) {
            text[0][y].setText("" + myLabel);
            try {
                Thread.sleep(500);
            }
            catch (Exception e) {
                e.printStackTrace();

            }   
            text[0][y].setText("");


        }
        for (int y=0; y < 1; y++) {
            text[0][y].setText("" + myLabel);
            try {
                Thread.sleep(500);
            }
            catch (Exception e) {
                e.printStackTrace();

            }   
            text[0][y].setText("");


        }
        */}
    }
}

}

0 个答案:

没有答案