能帮我看看是哪里出现问题了,是用错方法了吗?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class TicTacToe extends JFrame implements ActionListener {
Random random = new Random();
JPanel buttonPanel = new JPanel();
JLabel text = new JLabel();
JButton[] buttons = new JButton[9];
boolean playerTurn;
public TicTacToe() {
turn();
for(int i = 0; i < 9; i++) {
buttons[i] = new JButton();
buttonPanel.add(buttons[i], BorderLayout.CENTER);
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
buttons[i].setFocusable(false);
buttons[i].addActionListener(this);
}
//label
text.setHorizontalAlignment(JLabel.CENTER);
text.setText("Tic Tac Toe");
text.setOpaque(true);
//button
JButton jbtNewGame = new JButton("New Game");
JButton jbtPause = new JButton("Pause");
JButton jbtResume = new JButton("Resume");
//panel
JPanel title_panel = new JPanel();
title_panel.add(jbtNewGame);
title_panel.add(jbtPause);
title_panel.add(jbtResume);
add(title_panel, BorderLayout.NORTH);
//label
JLabel oWins = new JLabel("O wins:");
JLabel xWins = new JLabel("X wins:");
//wins
JPanel wins = new JPanel();
wins.add(oWins);
wins.add(xWins);
add(wins, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
for(int i = 0; i < 9; i++) {
if(e.getSource() == buttons[i]) {
if(playerTurn) {
if(buttons[i].getText() == "") {
buttons[i].setText("X");
playerTurn = false;
text.setText("O turn");
}
else {
if(buttons[i].getText() == "") {
buttons[i].setText("O");
playerTurn = true;
text.setText("X turn");
check();
}
}
}
}
}
}
public void turn() {
if(random.nextInt(2) == 0) {
playerTurn = true;
text.setText("X turn");
}else {
playerTurn = false;
text.setText("O turn");
}
}
public void check() {
//check X win conditions
if(
(buttons[0].getText() == "X") &&
(buttons[1].getText() == "X") &&
(buttons[2].getText() == "X")
) {
xWins(0, 1, 2);
}
if(
(buttons[3].getText() == "X") &&
(buttons[4].getText() == "X") &&
(buttons[5].getText() == "X")
) {
xWins(3, 4, 5);
}
if(
(buttons[6].getText() == "X") &&
(buttons[7].getText() == "X") &&
(buttons[8].getText() == "X")
) {
xWins(6, 7, 8);
}
if(
(buttons[0].getText() == "X") &&
(buttons[3].getText() == "X") &&
(buttons[6].getText() == "X")
) {
xWins(0, 3, 6);
}
if(
(buttons[1].getText() == "X") &&
(buttons[4].getText() == "X") &&
(buttons[7].getText() == "X")
) {
xWins(1, 4, 7);
}
if(
(buttons[2].getText() == "X") &&
(buttons[5].getText() == "X") &&
(buttons[8].getText() == "X")
) {
xWins(2, 5, 8);
}
if(
(buttons[0].getText() == "X") &&
(buttons[4].getText() == "X") &&
(buttons[8].getText() == "X")
) {
xWins(0, 4, 8);
}
if(
(buttons[2].getText() == "X") &&
(buttons[4].getText() == "X") &&
(buttons[6].getText() == "X")
) {
xWins(2, 4, 6);
}
//check O win conditions
if(
(buttons[0].getText() == "O") &&
(buttons[1].getText() == "O") &&
(buttons[2].getText() == "O")
) {
oWins(0, 1, 2);
}
if(
(buttons[3].getText() == "O") &&
(buttons[4].getText() == "O") &&
(buttons[5].getText() == "O")
) {
oWins(3, 4, 5);
}
if(
(buttons[6].getText() == "O") &&
(buttons[7].getText() == "O") &&
(buttons[8].getText() == "O")
) {
oWins(6, 7, 8);
}
if(
(buttons[0].getText() == "O") &&
(buttons[3].getText() == "O") &&
(buttons[6].getText() == "O")
) {
oWins(0, 3, 6);
}
if(
(buttons[1].getText() == "O") &&
(buttons[4].getText() == "O") &&
(buttons[7].getText() == "O")
) {
oWins(1, 4, 7);
}
if(
(buttons[2].getText() == "O") &&
(buttons[5].getText() == "O") &&
(buttons[8].getText() == "O")
) {
oWins(2, 5, 8);
}
if(
(buttons[0].getText() == "O") &&
(buttons[4].getText() == "O") &&
(buttons[8].getText() == "O")
) {
oWins(0, 4, 8);
}
if(
(buttons[2].getText() == "O") &&
(buttons[4].getText() == "O") &&
(buttons[6].getText() == "O")
) {
oWins(2, 4, 6);
}
}
public void xWins(int a, int b, int c) {
buttons[a].setBackground(Color.GREEN);
buttons[b].setBackground(Color.GREEN);
buttons[c].setBackground(Color.GREEN);
for(int i = 0; i < 9; i++) {
buttons[i].setEnabled(false);
}
text.setText("X wins");
}
public void oWins(int a, int b, int c) {
buttons[a].setBackground(Color.GREEN);
buttons[b].setBackground(Color.GREEN);
buttons[c].setBackground(Color.GREEN);
for(int i = 0; i < 9; i++) {
buttons[i].setEnabled(false);
}
text.setText("O wins");
}
public void beginGame() {
JOptionPane.showMessageDialog(null,"Now you can begin the game, have fun!");
}
public void exit() {
JOptionPane.showMessageDialog(null,"Thank you for playing! Bye");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
TicTacToe game = new TicTacToe();
int userInput;
String userChoice;
//Welcome screen and rules
JOptionPane.showMessageDialog(null, "Welcome to Tic Tac Toe Game");
JOptionPane.showMessageDialog(null, "Let me remind you the rule first:"
+ "\nThe user to get 3 of her/his marks in a row (up, down, across, or diagonally) is the winner."
+ "\nWhen all 9 squares are full, the game is over. "
+ "\nIf no player has 3 marks in a row, the game ends in a tie.");
//begin the game or exit
try {
JOptionPane.showMessageDialog(null,"Now please choice if you want begin the game or exit the game.");
JOptionPane.showMessageDialog(null,"1: Begin the game\n2: Exit");
userChoice = JOptionPane.showInputDialog("Now tell me your choice!");
userInput = Integer.parseInt(userChoice);
if("1".equals(userChoice)) {
game.beginGame();
TicTacToe frame = new TicTacToe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800,800);
frame.setVisible(true);
}else if("2".equals(userChoice)) {
game.exit();
}else {
throw new MyException("Please enter choice 1-2");
}
}
catch(MyException e) {
System.out.println(e);
}
}
}
buttonPanel没有丢进去
add(buttonPanel, BorderLayout.CENTER);