2017-07-19 63 views
0

這是一個tic tac腳趾遊戲,它工作正常。 但是,當我贏得比賽並再次點擊比賽時,比賽結束,但我意味着它再次開始。 我用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(); 

     } 
    } 
} 
} 
+0

爲什麼要放置窗口?只需清除GUI並重置所有變量。 – QBrute

+0

或再次調用'main'方法也應該可以正常工作 – XtremeBaumer

+0

以及如何清除gui? @QBrute –

回答

0

對於處置這裏看看: https://stackoverflow.com/a/13360489/3319324

你tictoe-變量m被殺死之後,你創造了它。所以不會有新的TicToe遊戲面板。

+0

我的要求是打開另一個窗口並關閉前一個窗口,我該怎麼做? –

+0

您可以實現主窗口和第二個遊戲窗口。所以如果你關閉遊戲窗口,主窗口仍然存在,你可以從中創建一個新的遊戲窗口。 –

相關問題