Java 使用Swing按钮重置应用程序
尝试使用“再次播放”按钮重置按钮Java 使用Swing按钮重置应用程序,java,swing,user-interface,jbutton,tic-tac-toe,Java,Swing,User Interface,Jbutton,Tic Tac Toe,尝试使用“再次播放”按钮重置按钮 import java.awt.*; 导入javax.swing.*; 导入java.awt.event.*; 导入java.awt.Toolkit; 导入java.awt.Image; 公共类HW4_tictoePanel扩展JFrame实现ActionListener{ //声明变量和数组 专用整数计数器=0; 私人字符串字母; 私有布尔赢=假; 私有int[][]winConditions=新int[][]{ {0,1,2},{3,4,5},{6,7,8}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.awt.Toolkit;
导入java.awt.Image;
公共类HW4_tictoePanel扩展JFrame实现ActionListener{
//声明变量和数组
专用整数计数器=0;
私人字符串字母;
私有布尔赢=假;
私有int[][]winConditions=新int[][]{
{0,1,2},{3,4,5},{6,7,8},//横向获胜
{0,3,6},{1,4,7},{2,5,8},//垂直获胜
{0,4,8},{2,4,6}//对角赢
};
//JFrame指令
//私有JButton按钮[]=新JButton[12];
//私有JFrame游戏窗口=新JFrame(“Tic Tac Toe”);
//端部框架
//面板演示
私有JPanel southPanel;//重播和退出按钮的面板
私有JButton[]optButtons;//退出和重播按钮
私人JPanel中心面板;
私有JButton[]游戏按钮;//Tic tac toe方块
私有网格布局myGridLayout;
//端面板演示
public HW4_tictoepanel()//构造函数
{
super(“Tic Tac Toe”);//super调用JFrame控制器
southPanel=newjpanel();//实例化面板
optButtons=newjbutton[2];//创建一个包含2个按钮的数组
字符串[]按钮名称={“再次播放”,“退出”};
centerPanel=newJPanel();//实例化面板
gameButtons=newjbutton[9];//创建一个包含9个按钮的数组
myGridLayout=新的GridLayout(3,3,2,2);
setLayout(new BorderLayout());//为JPanel设置布局
setLayout(myGridLayout);//设置中心面板的布局
//将按钮添加到中心面板并添加ActionListener
对于(int i=0;i 对于(int i=0;i,有那么多退出按钮,您只能退出。下面是一些代码改进:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;
import java.awt.Image;
public class HW4_TicTacToePanel extends JFrame implements ActionListener {
private int counter = 0;
private int [] [] winConditions = new int [] [] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Vertical wins
{0, 4, 8}, {2, 4, 6} // Diagonal wins
};
private JButton [] gameButtons;
public HW4_TicTacToePanel ()
{
super ("Tic Tac Toe");
JPanel southPanel = new JPanel ();
JButton [] optButtons = new JButton [2];
String [] buttonNames = {"Play Again", "Exit"};
JPanel centerPanel = new JPanel ();
gameButtons = new JButton [9];
GridLayout myGridLayout = new GridLayout (3, 3, 2, 2);
setLayout (new BorderLayout ());
centerPanel.setLayout (myGridLayout);
for (int i = 0; i < gameButtons.length; i++) {
gameButtons [i] = new JButton ();
centerPanel.add (gameButtons [i]);
gameButtons [i].addActionListener (this);
}
add (centerPanel, BorderLayout.CENTER);
for (int i = 0; i < optButtons.length; i++) {
optButtons [i] = new JButton (buttonNames [i]);
southPanel.add (optButtons [i]);
optButtons [i].addActionListener (this);
}
add (southPanel, BorderLayout.SOUTH);
setSize (300, 300);
setLocation (200, 200);
setVisible (true);
}
private static Icon [] icon = new ImageIcon [] {
new ImageIcon ("/home/stefan/some/images/mini.null.jpg"),
new ImageIcon ("/home/stefan/daten/some/images/x.gif")};
private static String [] letter = new String [] {"x", "o"};
public void actionPerformed (ActionEvent a) {
// Add Image icons
counter++;
boolean win = false;
String cmd = a.getActionCommand ();
if (cmd.equals ("Play Again"))
{
dispose ();
HW4_TicTacToePanel.main (null);
}
else if (cmd.equals ("Exit"))
{
dispose ();
} else {
// Calculate whose turn it is by using modulus to determine even or odd
JButton pressedButton = (JButton) a.getSource ();
pressedButton.setIcon (icon [counter%2]);
pressedButton.setText (letter[counter%2]);
pressedButton.setEnabled (false);
}
//determine who won
for (int i = 0; i <= 7; i++) {
if (gameButtons [winConditions [i] [0]].getText ().equals (gameButtons [winConditions [i] [1]].getText ())
& gameButtons [winConditions [i] [1]].getText ().equals (gameButtons [winConditions [i] [2]].getText ())
& gameButtons [winConditions [i] [0]].getText () != "") {
win = true;
}
}
//Show victor dialog
if (win == true) {
JOptionPane.showMessageDialog (null, letter[counter%2] + " wins the game!");
} else if (counter == 9 && win == false) {
JOptionPane.showMessageDialog (null, "The game was a tie!");
}
}
public static void main (String [] args) {
new HW4_TicTacToePanel ();
}
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.awt.Toolkit;
导入java.awt.Image;
公共类HW4_tictoePanel扩展JFrame实现ActionListener{
专用整数计数器=0;
私有int[][]winConditions=新int[][]{
{0,1,2},{3,4,5},{6,7,8},//横向获胜
{0,3,6},{1,4,7},{2,5,8},//垂直获胜
{0,4,8},{2,4,6}//对角赢
};
私有JButton[]游戏按钮;
公共HW4_tictoe面板()
{
超级(“Tic Tac Toe”);
JPanel-southPanel=newjpanel();
JButton[]optButtons=新JButton[2];
字符串[]按钮名称={“再次播放”,“退出”};
JPanel centerPanel=newjpanel();
gameButtons=新的JButton[9];
GridLayout myGridLayout=新的GridLayout(3,3,2,2);
setLayout(新的BorderLayout());
centerPanel.setLayout(myGridLayout);
对于(int i=0;iimport java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;
import java.awt.Image;
public class HW4_TicTacToePanel extends JFrame implements ActionListener {
private int counter = 0;
private int [] [] winConditions = new int [] [] {
{0, 1, 2}, {3, 4, 5}, {6, 7, 8}, // Horizontal wins
{0, 3, 6}, {1, 4, 7}, {2, 5, 8}, // Vertical wins
{0, 4, 8}, {2, 4, 6} // Diagonal wins
};
private JButton [] gameButtons;
public HW4_TicTacToePanel ()
{
super ("Tic Tac Toe");
JPanel southPanel = new JPanel ();
JButton [] optButtons = new JButton [2];
String [] buttonNames = {"Play Again", "Exit"};
JPanel centerPanel = new JPanel ();
gameButtons = new JButton [9];
GridLayout myGridLayout = new GridLayout (3, 3, 2, 2);
setLayout (new BorderLayout ());
centerPanel.setLayout (myGridLayout);
for (int i = 0; i < gameButtons.length; i++) {
gameButtons [i] = new JButton ();
centerPanel.add (gameButtons [i]);
gameButtons [i].addActionListener (this);
}
add (centerPanel, BorderLayout.CENTER);
for (int i = 0; i < optButtons.length; i++) {
optButtons [i] = new JButton (buttonNames [i]);
southPanel.add (optButtons [i]);
optButtons [i].addActionListener (this);
}
add (southPanel, BorderLayout.SOUTH);
setSize (300, 300);
setLocation (200, 200);
setVisible (true);
}
private static Icon [] icon = new ImageIcon [] {
new ImageIcon ("/home/stefan/some/images/mini.null.jpg"),
new ImageIcon ("/home/stefan/daten/some/images/x.gif")};
private static String [] letter = new String [] {"x", "o"};
public void actionPerformed (ActionEvent a) {
// Add Image icons
counter++;
boolean win = false;
String cmd = a.getActionCommand ();
if (cmd.equals ("Play Again"))
{
dispose ();
HW4_TicTacToePanel.main (null);
}
else if (cmd.equals ("Exit"))
{
dispose ();
} else {
// Calculate whose turn it is by using modulus to determine even or odd
JButton pressedButton = (JButton) a.getSource ();
pressedButton.setIcon (icon [counter%2]);
pressedButton.setText (letter[counter%2]);
pressedButton.setEnabled (false);
}
//determine who won
for (int i = 0; i <= 7; i++) {
if (gameButtons [winConditions [i] [0]].getText ().equals (gameButtons [winConditions [i] [1]].getText ())
& gameButtons [winConditions [i] [1]].getText ().equals (gameButtons [winConditions [i] [2]].getText ())
& gameButtons [winConditions [i] [0]].getText () != "") {
win = true;
}
}
//Show victor dialog
if (win == true) {
JOptionPane.showMessageDialog (null, letter[counter%2] + " wins the game!");
} else if (counter == 9 && win == false) {
JOptionPane.showMessageDialog (null, "The game was a tie!");
}
}
public static void main (String [] args) {
new HW4_TicTacToePanel ();
}
}