为什么处理不起作用?

时间:2017-07-19 06:24:39

标签: java jframe jdialog tic-tac-toe

这是一个tic tac toe游戏,它运行正常。 但是当我赢得比赛并再次点击比赛时,比赛结束但我打算再次开始。 我使用dispose关闭当前窗口并启动另一个窗口。 在我的其他游戏处理工作正常,但它不在这里。 我已经在消息函数中使用了dispose。 如果有人赢了比赛,我就打电话给消息功能。 编辑:我的要求是再次点击播放时他当前的游戏应该关闭,新的游戏应该开始。

package games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;



public class tictoe extends JFrame implements MouseListener
{
private static final Color COLOR_UNCLICKED = Color.white;
private static final Color COLOR_HITT= Color.CYAN;
private static final Color COLOR_HIT = Color.red;
private static final int UNCLICKED = 5;
private static final int HIT = 7;
private static final int HIT1 = 6;
private JLabel title;
private JPanel titlePanel;
private JButton[][] gridButton;
private JPanel gridPanel;
private final int ROWS = 3;
private final int COLS = 3;
private int[][] board;
int count=0;
GridListener gridListener = new GridListener();

Dimension boardSize = new Dimension(340, 400);
public tictoe()
{
    title = new JLabel("TIC TAC TOE");
    titlePanel = new JPanel();
    titlePanel.add(title);
    gridButton = new JButton[ROWS][COLS];
    board= new int [ROWS][COLS];
    gridPanel = new JPanel();
    gridPanel.setPreferredSize(boardSize);
    gridPanel.setLayout(new GridLayout(3, 3));
    for (int r = 0; r < gridButton.length; r++)
        for (int c = 0; c < gridButton[r].length; c++)
        {
        gridButton[r][c] = new JButton();
        //gridButton[r][c].setBackground(COLOR_UNCLICKED);
        gridButton[r][c].setEnabled(true);
        gridButton[r][c].addActionListener(gridListener);
        gridPanel.add(gridButton[r][c]);
        }
    for (int r = 0; r < board.length; r++)
        for (int c = 0; c < board.length; c++)
        {
        board[r][c] = UNCLICKED;
        gridButton[r][c].setEnabled(true);
        gridButton[r][c].setBackground(COLOR_UNCLICKED);
        }
    this.setLayout(new BorderLayout());
    this.add(titlePanel, "North");
    this.add(gridPanel, BorderLayout.CENTER);
    this.setPreferredSize(new Dimension(400, 400));
}

public static void main(String[] args) {
    tictoe n= new tictoe();
    n.pack();
    n.setVisible(true);
    n.setResizable(false);

}

@Override
public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
    // TODO Auto-generated method stub

}

 class GridListener implements ActionListener
{



public void actionPerformed(ActionEvent evt) {


        //System.out.println(count);

for (int r = 0; r < gridButton.length; r++)
for(int c = 0; c < gridButton[r].length; c++)
{

if (evt.getSource() != gridButton[r][c])
continue;
handleGridButton(r,c);
gridButton[r][c].setEnabled(false);
check();
return;
}
    }
}
 public void handleGridButton(int r, int c)
 {
 if (board[r][c] == UNCLICKED)
 {
     if(count%2==0)
     {
         board[r][c] = HIT;
         gridButton[r][c].setBackground(COLOR_HIT);
         gridButton[r][c].setEnabled(false);
         //((JButton)e.getSource()).setText("x");
     }

 else
 {

     board[r][c] = HIT1;
     gridButton[r][c].setBackground(COLOR_HITT);
     gridButton[r][c].setEnabled(false);
 }
 ++count;
 }
 }
 public void check()
 {
     if(board[0][0]==HIT && board[0][1]==HIT && board[0][2]==HIT)
         message();
     else if (board[0][0]==HIT1 && board[0][1]==HIT1 && board[0][2]==HIT1)
         message();
     else if (board[1][0]==HIT && board[1][1]==HIT && board[1][2]==HIT)
         message();
     else if (board[1][0]==HIT1 && board[1][1]==HIT1 && board[1][2]==HIT1)
         message();
     else if (board[2][0]==HIT && board[2][1]==HIT && board[2][2]==HIT)
         message();
     else if (board[2][0]==HIT1 && board[2][1]==HIT1 && board[2][2]==HIT1)
         message();
     else if(board[0][0]==HIT && board[1][0]==HIT && board[2][0]==HIT)
         message();
     else if(board[0][0]==HIT1 && board[1][0]==HIT1 && board[2][0]==HIT1)
         message();
     else if(board[0][1]==HIT && board[1][1]==HIT && board[2][1]==HIT)
         message();
     else if(board[0][1]==HIT1 && board[1][1]==HIT1 && board[2][1]==HIT1)
         message();
     else if(board[0][2]==HIT && board[1][2]==HIT && board[2][2]==HIT)
         message();
     else if(board[0][2]==HIT1 && board[1][2]==HIT1 && board[2][2]==HIT1)
         message();
     else if(board[0][0]==HIT && board[1][1]==HIT && board[2][2]==HIT)
         message();
     else if(board[0][0]==HIT1 && board[1][1]==HIT1 && board[2][2]==HIT1)
         message();
     else if(board[0][2]==HIT && board[1][1]==HIT && board[2][0]==HIT)
         message();
     else if(board[0][2]==HIT1 && board[1][1]==HIT1 && board[2][0]==HIT1)
         message();



 }
 public void message()
 {
     if(count%2==1)
     { Object[] options = { "Exit", "Play Again" };
     int choice = JOptionPane.showOptionDialog(null, 
         "Player 1 wins", 
         "Quit?", 
         JOptionPane.YES_NO_OPTION, 
         JOptionPane.QUESTION_MESSAGE, 
         null, 
         options, 
         options[0]);

     // interpret the user's choice
     if (choice == JOptionPane.YES_OPTION)
     {
       System.exit(0);
     }
     if (choice == JOptionPane.NO_OPTION)
     {
        dispose();
        tictoe m= new tictoe(); 

     } 
     }
     else
     {
         Object[] options = { "Exit", "Play Again" };
         int choice = JOptionPane.showOptionDialog(null, 
             "Player 2 wins", 
             "Quit?", 
             JOptionPane.YES_NO_OPTION, 
             JOptionPane.QUESTION_MESSAGE, 
             null, 
             options, 
             options[0]);

         // interpret the user's choice
         if (choice == JOptionPane.YES_OPTION)
         {
           System.exit(0);
         }
         if (choice == JOptionPane.NO_OPTION)
         {
           dispose();
            tictoe m= new tictoe(); 

         }
     }
 }
 }

1 个答案:

答案 0 :(得分:0)

有关处置的信息,请点击此处: https://stackoverflow.com/a/13360489/3319324

您的tictoe-variable m在您创建后立即被杀死。所以没有新的TicToe游戏面板。