Java创建一个新框架

时间:2014-04-28 03:01:28

标签: java swing constructor jframe

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."); 
 } 


} 

1 个答案:

答案 0 :(得分:2)

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

正如@MadProgrammer指出的那样,要求TicTac创建一个TicTac会产生一些陷阱22的情况:如何在没有第一个的情况下创建一个?如果由于某种原因,您确实需要创建一个TicTac来引用现有的TicTac,那么您需要创建一个没有参数的额外构造函数(正如您的编译器建议的那样)来创建第一个之一:

 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对于初学者来说可能是一个有用的读物​​。