public static void main(String[] arguments){
    TicTac frame = new TicTac();
}


第二个TicTac说找不到符号,并要求创建构造函数TicTac();

我的构造器如下:

public TicTac(TicTac in) {

}


我努力了

TicTac frame = new TicTac(TicTac in);


这是完整的代码:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import tictactoe.TicTac;


public class TicTac extends JFrame implements ItemListener, ActionListener, Runnable{
    TicTac gui;
    Thread playing;
    TicTacEvent tictac = new TicTacEvent(this);
    JPanel row1 = new JPanel();
    JButton[][] boxes = new JButton[3][3];
    JButton play = new JButton("Play");
    JTextField blank1 = new JTextField();
    JTextField blank2 = new JTextField();
    JOptionPane win = new JOptionPane("Winner");
    ImageIcon back = new ImageIcon("cardback.jpg");
    ImageIcon a = new ImageIcon("x.jpg");
    ImageIcon b = new ImageIcon("o.jpg");
    int clicks = 0;
    //int win = 0;
    int winX = 0;
    int winO = 0;
    int tie = 0;
    int[][] check = new int[3][3];

    public TicTac(TicTac in) {
        super ("Tic Tac Toe");
        gui = in;
        setSize (500,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout layout = new FlowLayout();
        setLayout(layout);
        int name = 0;
        String newname;

        for (int row=0; row<=2;row++){
            for(int col=0; col<=2; col++){
                check[row][col]=0;
            }
        }

        GridLayout layout1 = new GridLayout(4, 3, 10, 10);
        row1.setLayout(layout1);
        for (int x=0; x<=2; x++){
            for (int y=0; y<=2; y++){
                name = name + 1;
                newname = Integer.toString(name);
                boxes[x][y] = new JButton(newname);
                boxes[x][y].setIcon(back);
                row1.add(boxes[x][y]);
            }
        }
        row1.add(blank1);
        row1.add(play);
        row1.add(blank2);
        add (row1);

        play.addActionListener(tictac);
        for (int x=0; x<=2; x++){
            for (int y=0; y<=2; y++){
                boxes[x][y].addActionListener(tictac);
            }
        }


        setVisible(true);
    }

    public static void main(String[] arguments){
        TicTac frame = new TicTac();
    }

    public void itemStateChanged(ItemEvent ie) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void actionPerformed(ActionEvent event) {
       String command = event.getActionCommand();

       if (command.equals("Play")) {
           startPlaying();
       }
       if (command.equals("1")) {
           b1();
       }
       if (command.equals("2")) {
           b2();
       }
       if (command.equals("3")) {
           b3();
       }
       if (command.equals("4")) {
           b4();
       }
       if (command.equals("5")) {
           b5();
       }
       if (command.equals("6")) {
           b6();
       }
       if (command.equals("7")) {
           b7();
       }
       if (command.equals("8")) {
           b8();
       }
       if (command.equals("9")) {
           b9();
       }
    }

    void b1() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[0][0].setIcon(a);
            check[0][0] = 1;
        } else {
            gui.boxes[0][0].setIcon(b);
            check[0][0] = 2;
        }
        winner();

    }
    void b2() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[0][1].setIcon(a);
            check[0][1] = 1;
        } else {
            gui.boxes[0][1].setIcon(b);
            check[0][1] = 2;
        }
        winner();
    }
    void b3() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[0][2].setIcon(a);
            check[0][2] = 1;
        } else {
            gui.boxes[0][2].setIcon(b);
            check[0][2] = 2;
        }
        winner();
    }
    void b4() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[1][0].setIcon(a);
            check[1][0] = 1;
        } else {
            gui.boxes[1][0].setIcon(b);
            check[1][0] = 2;
        }
        winner();
    }
    void b5() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[1][1].setIcon(a);
            check[1][1] = 1;
        } else {
            gui.boxes[1][1].setIcon(b);
            check[1][1] = 2;
        }
        winner();
    }
    void b6() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[1][2].setIcon(a);
            check[1][2] = 1;
        } else {
            gui.boxes[1][2].setIcon(b);
            check[1][2] = 2;
        }
        winner();
    }
    void b7() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[2][0].setIcon(a);
            check[2][0] = 1;
        } else {
            gui.boxes[2][0].setIcon(b);
            check[2][0] = 2;
        }
        winner();
    }
    void b8() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[2][1].setIcon(a);
            check[2][1] = 1;
        } else {
            gui.boxes[2][1].setIcon(b);
            check[2][1] = 2;
        }
        winner();
    }
    void b9() {
        clicks = clicks + 1;
        if ((clicks%2)==1){
            gui.boxes[2][2].setIcon(a);
            check[2][2] = 1;
        } else {
            gui.boxes[2][2].setIcon(b);
            check[2][2] = 2;
        }
        winner();
    }

    void winner() {
        /** Check rows for winner */

        for (int x=0; x<=2; x++){
            if ((check[x][0]==check[x][1])&&(check[x][0]==check[x][2])) {
                if (check[x][0]==1) {
                    JOptionPane.showMessageDialog(null, "X is the winner");
                    winX = 1;
                    System.out.println(Integer.toString(winX));
                    blank1.setText("Number of X Wins = " + (Integer.toString(winX)));
                } else if (check[x][0]==2){
                    JOptionPane.showMessageDialog(null, "O is the winner");
                    winO = 1;
                }
            }
        }

        /** Check columns for winner */
        for (int x=0; x<=2; x++){
            if ((check[0][x]==check[1][x])&&(check[0][x]==check[2][x])) {
                if (check[0][x]==1) {
                    JOptionPane.showMessageDialog(null, "X is the winner");
                    winX = 1;
                } else if (check[0][x]==2) {
                    JOptionPane.showMessageDialog(null, "O is the winner");
                    winO = 1;
                }
            }
        }

        /** Check diagonals for winner */
        if (((check[0][0]==check[1][1])&&(check[0][0]==check[2][2]))||
                ((check[2][0]==check[1][1])&&(check[1][1]==check[0][2]))){
            if (check[1][1]==1) {
                JOptionPane.showMessageDialog(null, "X is the winner");
                winX = 1;
            } else if (check[1][1]==2) {
                JOptionPane.showMessageDialog(null, "O is the winner");
                winO = 1;
            }

        }

        /** Checks if the game is a tie */
       /* if ((clicks==9) && (win==0)) {
            JOptionPane.showMessageDialog(null, "The game is a tie");
        }
    */}


    void startPlaying() {
        playing = new Thread(this);
        playing.start();
        gui.play.setEnabled(false);
    }


    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}


为了解决我的问题,我创建了一个单独的类。
第一类:

import java.awt.*;
import javax.swing.*;

public class TicTac extends JFrame {
 TicTacEvent tictac = new TicTacEvent(this);

 JPanel row1 = new JPanel();
 JButton[][] boxes = new JButton[3][3];
 JButton play = new JButton("Play");
 JTextField blank1 = new JTextField();
 JTextField blank2 = new JTextField();
 JOptionPane win = new JOptionPane("Winner");
 ImageIcon back = new ImageIcon("cardback.jpg");


 public TicTac() {
 super ("Tic Tac Toe");
 setSize (500,600);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 FlowLayout layout = new FlowLayout();
 setLayout(layout);
 int name = 0;
 String newname;

 GridLayout layout1 = new GridLayout(4, 3, 10, 10);
 row1.setLayout(layout1);
 for (int x=0; x<=2; x++){
 for (int y=0; y<=2; y++){
 name = name + 1;
 newname = Integer.toString(name);
 boxes[x][y] = new JButton(newname);
 boxes[x][y].setIcon(back);
 row1.add(boxes[x][y]);
 }
 }
 row1.add(blank1);
 row1.add(play);
 row1.add(blank2);
 add (row1);

 play.addActionListener(tictac);
 for (int x=0; x<=2; x++){
 for (int y=0; y<=2; y++){
 boxes[x][y].addActionListener(tictac);
 }
 }

 setVisible(true);
 }

 public static void main(String[] arguments){
 TicTac frame = new TicTac();
 }
}


第2类:

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class TicTacEvent implements ItemListener, ActionListener, Runnable {
 TicTac gui;
 Thread playing;
 ImageIcon a = new ImageIcon("x.jpg");
 ImageIcon b = new ImageIcon("o.jpg");
 int clicks = 0;
 int win = 0;
 int[][] check = new int[3][3];

 public TicTacEvent (TicTac in){
 gui = in;
 for (int row=0; row<=2; row++){
 for (int col=0; col<=2; col++){
 check[row][col]=0;
 }
 }
 }

 public void actionPerformed (ActionEvent event) {
 String command = event.getActionCommand();

 if (command.equals("Play")) {
 startPlaying();
 }
 if (command.equals("1")) {
 b1();
 }
 if (command.equals("2")) {
 b2();
 }
 if (command.equals("3")) {
 b3();
 }
 if (command.equals("4")) {
 b4();
 }  if (command.equals("5")) {
 b5();
 }
 if (command.equals("6")) {
 b6();
 }
 if (command.equals("7")) {
 b7();
 }
 if (command.equals("8")) {
 b8();
 }
 if (command.equals("9")) {
 b9();
 }
 }

 void b1() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[0][0].setIcon(a);
 check[0][0] = 1;
 } else {
 gui.boxes[0][0].setIcon(b);
 check[0][0] = 2;
 }
 winner();

 }
 void b2() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[0][1].setIcon(a);
 check[0][1] = 1;
 } else {
 gui.boxes[0][1].setIcon(b);
 check[0][1] = 2;
 }
 winner();
 }
 void b3() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[0][2].setIcon(a);
 check[0][2] = 1;
 } else {  gui.boxes[0][2].setIcon(b);
 check[0][2] = 2;
 }
 winner();
 }
 void b4() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[1][0].setIcon(a);
 check[1][0] = 1;
 } else {
 gui.boxes[1][0].setIcon(b);
 check[1][0] = 2;
 }
 winner();
 }
 void b5() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[1][1].setIcon(a);
 check[1][1] = 1;
 } else {
 gui.boxes[1][1].setIcon(b);
 check[1][1] = 2;
 }
 winner();
 }
 void b6() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[1][2].setIcon(a);
 check[1][2] = 1;
 } else {
 gui.boxes[1][2].setIcon(b);
 check[1][2] = 2;
 }
 winner();
 }
 void b7() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[2][0].setIcon(a);
 check[2][0] = 1;
 } else {
 gui.boxes[2][0].setIcon(b);
 check[2][0] = 2;  }
 winner();
 }
 void b8() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[2][1].setIcon(a);
 check[2][1] = 1;
 } else {
 gui.boxes[2][1].setIcon(b);
 check[2][1] = 2;
 }
 winner();
 }
 void b9() {
 clicks = clicks + 1;
 if ((clicks%2)==1){
 gui.boxes[2][2].setIcon(a);
 check[2][2] = 1;
 } else {
 gui.boxes[2][2].setIcon(b);
 check[2][2] = 2;
 }
 winner();
 }

 void winner() {
 /** Check rows for winner */

 for (int x=0; x<=2; x++){
 if ((check[x][0]==check[x][1])&&(check[x][0]==check[x][2])) {
 if (check[x][0]==1) {
 JOptionPane.showMessageDialog(null, "X is the winner");
 win = 1;
 } else if (check[x][0]==2){
 JOptionPane.showMessageDialog(null, "Y is the winner");
 win = 1;
 }
 }
 }

 /** Check columns for winner */
 for (int x=0; x<=2; x++){
 if ((check[0][x]==check[1][x])&&(check[0][x]==check[2][x])) {
 if (check[0][x]==1) {
 JOptionPane.showMessageDialog(null, "X is the winner");  win = 1;
 } else if (check[0][x]==2) {
 JOptionPane.showMessageDialog(null, "Y is the winner");
 win = 1;
 }
 }
 }

 /** Check diagonals for winner */
 if (((check[0][0]==check[1][1])&&(check[0][0]==check[2][2]))||
 ((check[2][0]==check[1][1])&&(check[1][1]==check[0][2]))){
 if (check[1][1]==1) {
 JOptionPane.showMessageDialog(null, "X is the winner");
 win = 1;
 } else if (check[1][1]==2) {
 JOptionPane.showMessageDialog(null, "Y is the winner");
 win = 1;
 }

 }

 /** Checks if the game is a tie */
 if ((clicks==9) && (win==0)) {
 JOptionPane.showMessageDialog(null, "The game is a tie");
 }
 }


 void startPlaying() {
 playing = new Thread(this);
 playing.start();
 gui.play.setEnabled(false);
 }

 public void itemStateChanged(ItemEvent e) {
 throw new UnsupportedOperationException("Not supported yet.");
 }

 public void run() {
 throw new UnsupportedOperationException("Not supported yet.");
 }


}

最佳答案

您的构造函数要求您传入现有的TicTac作为参数。在显示的代码中,您尝试不创建TicTac

正如@MadProgrammer指出的那样,要求创建TicTacTicTac会导致22类情况:如何才能不先创建一个?如果由于某种原因确实需要创建一个引用现有TicTacTicTac,则需要创建一个没有参数的附加构造函数(如编译器所建议的那样)来创建第一个:

 public TicTac() {  }


(更准确地说,您的构造函数可以接受参数,只需要TicTac即可):

class TicTac {
    public TicTac(int size) { }
    public TicTac(TicTac otherTicTac) { }
    ...
 }
 //elsewhere:
 TicTac first = new TicTac(5);  //or just TicTac first = new TicTac()
 TicTac second = new TicTac(first);


但是,我猜测您实际上并不需要TicTac(TicTac otherTicTac)构造函数(如果是,请告诉我原因),并且您由于对构造函数的工作方式有误解而试图这样做-在这种情况下对于初学者来说,this可能是有用的读物​​。

10-04 15:19