gpt4 book ai didi

Java创建一个新框架

转载 作者:行者123 更新时间:2023-12-02 05:53:27 25 4
gpt4 key购买 nike

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 指出的,需要一个 TicTac 来创建一个 TicTac 会造成某种第 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对于初学者来说,这可能是一本有用的读物​​。

关于Java创建一个新框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23331845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com