好了,所以我想學習Java的GUI,並在本書中,我曾經有過(這是6歲),我試圖複製他們的井字遊戲的一個樣本。 java的獲得空白的舊式Java GUI井字棋
無論所使用的語句是有點老了,或者我需要知道出門前,並試圖這些東西有些事,我真的不能沒有理由出現在屏幕上搞清楚。
這裏是我的代碼(原諒奇數名稱和文本):
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);
}
}
我做了「伊修」 x值和「Johpit」鄰出於好奇和樂趣的值。
不管怎麼說,我是新來的GUI的Java和我想試試這個,這樣我可以再瞭解它的每一部分慢。所有這些代碼顯示的是一個空白窗口。有任何想法嗎?
謝謝:)
你永遠不添加任何東西到窗口。 – DejaVuSansMono