程序中只有一个class(extends Applet), 运行一遍之后该怎么做才能重新运行这个Class呢?附上代码,红字部分import java.awt.*;
import javax.swing.*;
import java.awt.event.*; public class Minesweeper extends JApplet implements ActionListener {
//The number of button
int NUM = 9;
// Set a number to get the random number which is to set bomb
int random;
// Set array of button
JButton[] button = new JButton[NUM];
// Set the bomb button
JButton bomb = new JButton();
// Initial the total number of opening no-bomb buttons
int total = 0;
public void init() {
String welcome = "Welcome to Mini-Minesweeper!\nThe Obeject of the game is to click on all\n" +
"the squares EXCEPT the one with the bomb\n(There is only one bomb). To choose" +
"a square\n to display please simply click on the square";
JOptionPane.showMessageDialog(null, welcome, "Message", JOptionPane.INFORMATION_MESSAGE);
// Set a GridLayout of 3 columns and 3 rows
setLayout(new GridLayout(3, 3));
// Set initial background of buttons and add them to the frame
for (int i = 0; i < NUM; i++) {
button[i] = new JButton();
button[i].setBackground(Color.gray);
getContentPane().add(button[i]);
}
setBomb();
// Register listeners
for (int i = 0; i < NUM; i++)
button[i].addActionListener(this);
}
/** Set bomb randomly */
public void setBomb() {
int randomNum = (int) (Math.random() * 9);
bomb = button[randomNum];
random = randomNum;
System.out.println(random);
}
/** This method will be invoked when buttons are clicked*/
public void actionPerformed(ActionEvent e) {
// The state of clicking bomb
if (e.getSource() == bomb) {
// Set the button to black if click bomb
bomb.setBackground(Color.black);
//Display the losing dialog
Object[] options = {"Yes","No"};
//return 1 to ans when user press the No button
int ans = JOptionPane.showOptionDialog( null, "BOOoom!!! Would you like to play again?", " " , JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (ans == 1)
System.exit(0); // exit the game
else {
重新游戏,即要再运行一遍class
}
// The state of clicking not-bomb button
for (int j = 0; j < NUM; j++) {
if (e.getSource() == button[j] && j != random) {
// Avoid clicking the same button in many times
if(button[j].getBackground().equals(Color.gray)){
total ++;
// Set the button to black if click a not-bomb button
button[j].setBackground(Color.white);
}
}
// The state that only one square is left
if (total == 8) {
//Display the winning dialog
Object[] options = {"Yes","No"};
//return 1 to ans when user press the No button
int ans = JOptionPane.showOptionDialog(null, "Win!!! Would you like to play again?", "", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (ans == 1)
System.exit(0); // exit the game
else {
重新游戏,即要再运行一遍class
} }
}
} }
import javax.swing.*;
import java.awt.event.*; public class Minesweeper extends JApplet implements ActionListener {
//The number of button
int NUM = 9;
// Set a number to get the random number which is to set bomb
int random;
// Set array of button
JButton[] button = new JButton[NUM];
// Set the bomb button
JButton bomb = new JButton();
// Initial the total number of opening no-bomb buttons
int total = 0;
public void init() {
String welcome = "Welcome to Mini-Minesweeper!\nThe Obeject of the game is to click on all\n" +
"the squares EXCEPT the one with the bomb\n(There is only one bomb). To choose" +
"a square\n to display please simply click on the square";
JOptionPane.showMessageDialog(null, welcome, "Message", JOptionPane.INFORMATION_MESSAGE);
// Set a GridLayout of 3 columns and 3 rows
setLayout(new GridLayout(3, 3));
// Set initial background of buttons and add them to the frame
for (int i = 0; i < NUM; i++) {
button[i] = new JButton();
button[i].setBackground(Color.gray);
getContentPane().add(button[i]);
}
setBomb();
// Register listeners
for (int i = 0; i < NUM; i++)
button[i].addActionListener(this);
}
/** Set bomb randomly */
public void setBomb() {
int randomNum = (int) (Math.random() * 9);
bomb = button[randomNum];
random = randomNum;
System.out.println(random);
}
/** This method will be invoked when buttons are clicked*/
public void actionPerformed(ActionEvent e) {
// The state of clicking bomb
if (e.getSource() == bomb) {
// Set the button to black if click bomb
bomb.setBackground(Color.black);
//Display the losing dialog
Object[] options = {"Yes","No"};
//return 1 to ans when user press the No button
int ans = JOptionPane.showOptionDialog( null, "BOOoom!!! Would you like to play again?", " " , JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (ans == 1)
System.exit(0); // exit the game
else {
重新游戏,即要再运行一遍class
}
// The state of clicking not-bomb button
for (int j = 0; j < NUM; j++) {
if (e.getSource() == button[j] && j != random) {
// Avoid clicking the same button in many times
if(button[j].getBackground().equals(Color.gray)){
total ++;
// Set the button to black if click a not-bomb button
button[j].setBackground(Color.white);
}
}
// The state that only one square is left
if (total == 8) {
//Display the winning dialog
Object[] options = {"Yes","No"};
//return 1 to ans when user press the No button
int ans = JOptionPane.showOptionDialog(null, "Win!!! Would you like to play again?", "", JOptionPane.NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
if (ans == 1)
System.exit(0); // exit the game
else {
重新游戏,即要再运行一遍class
} }
}
} }
new Minesweeper()来真正的重新开始了。