好的,所以我想在Java上学习GUI,在这本书上我曾经(已经6岁了),我试图复制他们的TicTacToe.java样本
使用的语句是否有点陈旧,或者我在出去尝试这些东西之前需要了解一些事情,我真的无法弄清楚为什么屏幕上没有任何内容。
这是我的代码(原谅奇怪的名字和文字):
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JButton;
import javax.swing.JFrame;
public class FranzTicTacToe extends JFrame implements ActionListener
{
private Container c = getContentPane();
private JButton[][] gameBoard = new JButton[3][3];
private ImageIcon imageo = new ImageIcon("C:\\Users\\hftri\\Downloads\\tic.jpg");
private ImageIcon imagex = new ImageIcon("C:\\Users\\hftri\\Downloads\\tac.jpg");
private String turn = "Aloysius";
boolean isGameOver = false;
public void TicTacToe()
{
c = getContentPane();
gameBoard = new JButton[3][3];
imageo = new ImageIcon("C:\\Users\\hftri\\Downloads\\tic.jpg");
imagex = new ImageIcon("C:\\Users\\hftri\\Downloads\\tac.jpg");
isGameOver = false;
c.setLayout(new GridLayout(3,3));
setTitle("Harambe");
setBounds(250, 250, 300, 300);
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
gameBoard[column][row] = new JButton();
c.add(gameBoard[column][row]);
gameBoard[column][row].addActionListener(this);
}
}
}
public void actionPerformed(ActionEvent e)
{
if(isGameOver)
return;
JButton pressedButton = (JButton)e.getSource();
int pressedColumn = -1, pressedRow = -1;
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
if(gameBoard[column][row] == pressedButton)
{
pressedColumn = column;
pressedRow = row;
}
}
}
//Game START
if(gameBoard[pressedColumn][pressedRow].getIcon() != null)
{
return;
}
if(turn.equals("Aloysius"))
{
gameBoard[pressedColumn][pressedRow].setIcon(imagex);
turn = "Johpit";
}
else
{
gameBoard[pressedColumn][pressedRow].setIcon(imageo);
turn = "Aloysius";
}
if(checkRow(pressedRow, imageo) || checkColumn(pressedColumn, imageo) || checkDiagonals(imageo))
{
isGameOver = true;
JOptionPane.showMessageDialog(FranzTicTacToe.this, "Aloysius is skrub", "Result", JOptionPane.INFORMATION_MESSAGE);
turn = "Johpit";
playAgain();
}
if(checkRow(pressedRow, imagex) || checkColumn(pressedColumn, imagex) || checkDiagonals(imagex))
{
isGameOver = true;
JOptionPane.showMessageDialog(FranzTicTacToe.this, "Johpit is skrub", "Result", JOptionPane.INFORMATION_MESSAGE);
turn = "Aloysius";
playAgain();
}
if(!isGameOver)
{
boolean tie = true;
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
if(gameBoard[column][row].getIcon() == null)
{
tie = false;
break;
}
}
}
if(tie)
{
JOptionPane.showMessageDialog(FranzTicTacToe.this, "Both of you are fukin skrubs", "Result", JOptionPane.INFORMATION_MESSAGE);
isGameOver = true;
playAgain();
}
}
if(!isGameOver)
{
setTitle(turn + "'s turn");
}
}
boolean checkRow(int row, Icon player)
{
for(int column=0; column < 3; column++)
{
if(gameBoard[column][row].getIcon() != player)
{
return false;
}
}
return true;
}
boolean checkColumn(int column, Icon player)
{
for(int row=0; row < 3; row++)
{
if(gameBoard[column][row].getIcon() != player)
{
return false;
}
}
return true;
}
boolean checkDiagonals(Icon player)
{
if(gameBoard[1][1].getIcon() != player)
return false;
if(gameBoard[0][0].getIcon() == player && gameBoard[2][2].getIcon() == player)
{
return true;
}
return false;
}
//Game Restarting Option
void playAgain()
{
Object[] options = {"Yes", "No"};
int n = JOptionPane.showOptionDialog(FranzTicTacToe.this, "Pusta again?", "Query", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if(n == JOptionPane.YES_OPTION)
{
dispose();
FranzTicTacToe game = new FranzTicTacToe();
game.setVisible(true);
}
else
{
System.exit(0);
}
}
public static void main(String[] args)
{
FranzTicTacToe game = new FranzTicTacToe();
game.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
game.setVisible(true);
}
}
我将“Aloysius”作为x的值,将“Johpit”作为好奇和乐趣的值。
无论如何,我是Java的GUI新手,我想尝试这个,这样我就可以慢慢学习它的每一部分。所有这些代码显示的是一个空白窗口。有什么想法吗?
谢谢:)
答案 0 :(得分:2)
您缺少构造函数。现在它使用默认的构造函数,它基本上什么都不做。它可能是一个拼写错误,但为了理解。如果没有声明构造函数,它将使用Object
类中的默认构造函数
构造函数还需要具有类的确切名称。因此,将TicTacToe
方法更改为FranzTicTacToe
构造函数
public void TicTacToe() // needs to be public FranzTicTacToe()
{
c = getContentPane();
gameBoard = new JButton[3][3];
imageo = new ImageIcon("C:\\Users\\hftri\\Downloads\\tic.jpg");
imagex = new ImageIcon("C:\\Users\\hftri\\Downloads\\tac.jpg");
isGameOver = false;
c.setLayout(new GridLayout(3,3));
setTitle("Harambe");
setBounds(250, 250, 300, 300);
for(int column=0; column < 3; column++)
{
for(int row=0; row < 3; row++)
{
gameBoard[column][row] = new JButton();
c.add(gameBoard[column][row]);
gameBoard[column][row].addActionListener(this);
}
}
}
答案 1 :(得分:0)
您重命名了该类,但忘记重命名构造函数,然后以错误的方式修复它。长话短说:
public void TicTacToe()
应该是
public FranzTicTacToe()