我是一名AP计算机科学专业的学生,我需要帮助完成课程作业。我的任务是使用Eclipse创建一个简单的GUI或游戏。我做了一个简单的玩家对玩家tic-tac-toe游戏,但我不知道如何为我的GUI创建一个“重置”按钮。我尝试了多次,但我无法让它工作或显示在我的GUI中。我会很感激有关如何实现功能重置按钮的一些指示,所以我不会多次退出GUI以重新开始播放。这是我到目前为止编写的代码。
package gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TicTacToeGUI implements ActionListener
{
JFrame window = new JFrame("Tic-Tac-Toe");
JButton[] button;
JButton reset = new JButton("Reset");
String letter = "";
public int count = 0;
public boolean win = false;
public TicTacToeGUI()
{
button = new JButton[9];
window.setSize(300,300);
window.setLayout(new GridLayout(3,3));
JButton dummy = new JButton("");
Font font = dummy.getFont();
Font bigFont = font.deriveFont(font.getSize2D() * 5.0f);
JButton reset = new JButton("Reset");
for(int i = 0; i < 9; i++)
{
button[i] = new JButton("");
button[i].setFont(bigFont);
button[i].addActionListener(this);
window.add(button[i]);
}
window.setVisible(true);
}
public void actionPerformed(ActionEvent a)
{
count++;
if(count % 2 == 1)
{
letter = "X";
}
else
{
letter = "O";
}
Object but = a.getSource();
for(int i = 0; i < 9; i++)
{
if(but == button[i])
{
button[i].setText(letter);
button[i].setEnabled(false);
break;
}
}
if( button[0].getText() == button[1].getText() && button[1].getText() == button[2].getText() && button[0].getText() != "")
{
win = true;
}
else if(button[3].getText() == button[4].getText() && button[4].getText() == button[5].getText() && button[3].getText() != "")
{
win = true;
}
else if(button[6].getText() == button[7].getText() && button[7].getText() == button[8].getText() && button[6].getText() != "")
{
win = true;
}
else if(button[0].getText() == button[3].getText() && button[3].getText() == button[6].getText() && button[0].getText() != "")
{
win = true;
}
else if(button[1].getText() == button[4].getText() && button[4].getText() == button[7].getText() && button[1].getText() != "")
{
win = true;
}
else if(button[2].getText() == button[5].getText() && button[5].getText() == button[8].getText() && button[2].getText() != "")
{
win = true;
}
else if(button[0].getText() == button[4].getText() && button[4].getText() == button[8].getText() && button[0].getText() != "")
{
win = true;
}
else if(button[2].getText() == button[4].getText() && button[4].getText() == button[6].getText() && button[2].getText() != "")
{
win = true;
}
else
{
win = false;
}
if(win == true)
{
JOptionPane.showMessageDialog(null, letter + " WINS!");
}
else if(count == 9 && win == false)
{
JOptionPane.showMessageDialog(null, "Tie Game!");
}
}
public static void main(String[] args)
{
new TicTacToeGUI();
}
}
答案 0 :(得分:0)
您可能想尝试一下:
window.setLayout(new BorderLayout());
JPanel panel = new JPanel( new GridLayout(3, 3));
window.add(panel, BorderLayout.CENTER); // add panel to window center
window.add(reset, BorderLayout.SOUTH); // add reset button to window bottom
当然,您现在必须将9个按钮添加到panel
,而不是window
。
但是,为什么不在用户在游戏结束时确认对话后自动重置?
答案 1 :(得分:0)
在平局或胜利后重置你的牌。一个示例重置方法。否则你将不得不在你的Frame上腾出空间来按住按钮来执行此操作。
if (isBalloon) {
if (isRed) {
System.out.println("Red balloon");
} else {
System.out.println("Balloon");
}
} else {
System.out.println("Not a balloon");
}
然后使用此方法进行检查以确定玩家是赢了还是游戏以平局结束如下:
if (isBalloon && isRed) {
System.out.println("Red balloon");
} else if (isBalloon) {
System.out.println("Balloon");
} else {
System.out.println("Not a balloon");
}