import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;class Sweeper extends JFrame {
private static int N = 9; JButton btn[] = new JButton[N];
JButton btnY, btnN;
Random rnd = new Random();
int count = 0, n, x = rnd.nextInt(9); Sweeper(String ss) {
super(ss);
JOptionPane
.showMessageDialog(
this,
"Welcome to Mini-MineSweeper!\n The object of the game is to click on all\nthe squares EXCEPT the one with the bomb.\n(There is only one bomb).To choose a square\nto display please simply click on the square.",
"Message", JOptionPane.INFORMATION_MESSAGE);
// 开始的对话窗 int i; for (i = 0; i < N; i++) {
btn[i] = new JButton();
btn[i].setBackground(Color.lightGray);
btn[i].addActionListener(new handlerClass()); // 为按钮添加ActionEvent事件
} Container frm = getContentPane(); // 初始化frm容器
frm.setLayout(new GridLayout(3, 3)); // 设置frm容器为BorderLayout布局(区域间隔10)
for (i = 0; i < N; i++)
frm.add(btn[i]);
frm.validate(); // 验证frm容器及其所有子组件 setSize(370, 180); // 设置窗口大小
setResizable(false); // 设置窗口为不可调整大小
setLocationRelativeTo(null); // 设置窗口位于屏幕中央
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置在窗口上发起 "close"
// 时默认执行的操作为退出应用程序
setVisible(true); // 设置窗口可见
} class handlerClass extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent ee) {
int j;
for (j = 0; j < N; j++) {
if (ee.getSource() == btn[j]) {
if (x == j) {
btn[j].setBackground(Color.black);
count = 0; // 加入哭脸 cryFace frame1 = new cryFace();
frame1.run();
Thread t1=new Thread(frame1);
t1.start();
System.out.println("开始:"+t1.isAlive());
try {
Thread.sleep(3000);
System.out.println("睡眠:"+t1.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
frame1.shutDown();
System.out.println("结束:"+t1.isAlive());
// 哭脸结束
n = JOptionPane.showConfirmDialog(this,
"BOOooM!!!Would you like to play again?", " ",
JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) {
for (j = 0; j < N; j++)
btn[j].setBackground(Color.lightGray);
x = rnd.nextInt(9);
} else
System.exit(0);
} else {
btn[j].setBackground(Color.white);
count++;
}
if (count == 8) {
// 加入笑脸
smileface frame2 = new smileface();
Thread t2=new Thread(frame2);
t2.start();
frame2.run();
System.out.println("开始:"+t2.isAlive());
try {
Thread.sleep(3000);
System.out.println("睡眠:"+t2.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
frame2.shutDown();
System.out.println("结束:"+t2.isAlive());
n = JOptionPane.showConfirmDialog(this,
"You win!!!Would you like to play again?", " ",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) { for (j = 0; j < N; j++)
btn[j].setBackground(Color.lightGray);
x = rnd.nextInt(9);
} else
System.exit(0);
}
}
}
} }}public class MiniMineSweeper {
public static void main(String[] args) {
Sweeper sweep = new Sweeper("Boom!"); // 初始化sweep(Sweeper方法) }
}// 哭脸的
class cryFace extends JFrame implements Runnable{
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, 370, 180); g.setColor(Color.red);
g.fillOval(111, 30, 23, 23);
g.fillOval(222, 30, 23, 23);
g.drawArc(115, 80, 121, 90, 10, 160);
}
public void run() {
setTitle("Boom!");
setSize(370,180);
setLocationRelativeTo(null);
setVisible(true);
}
public void shutDown(){
this.dispose();
}
}
//笑脸
class smileface extends JFrame implements Runnable{
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillRect(0, 0, 370, 180);
g.setColor(Color.red);
g.fillOval(111, 30, 23, 23);
g.fillOval(222, 30, 23, 23);
g.drawArc(115, 25, 121, 90, 200, 145);
}
public void run() {
setTitle("Boom!");
setSize(370, 180);
setLocationRelativeTo(null);
setVisible(true);
}
public void shutDown(){
this.dispose();
}}
这个程序问题出在,t.start()进程却看不到笑、哭图象,但是整个程序不报告错误?? 请高手帮忙修正一下,小弟先谢谢了? ?/
import java.awt.event.*;
import java.util.*;
import javax.swing.*;class Sweeper extends JFrame {
private static int N = 9; JButton btn[] = new JButton[N];
JButton btnY, btnN;
Random rnd = new Random();
int count = 0, n, x = rnd.nextInt(9); Sweeper(String ss) {
super(ss);
JOptionPane
.showMessageDialog(
this,
"Welcome to Mini-MineSweeper!\n The object of the game is to click on all\nthe squares EXCEPT the one with the bomb.\n(There is only one bomb).To choose a square\nto display please simply click on the square.",
"Message", JOptionPane.INFORMATION_MESSAGE);
// 开始的对话窗 int i; for (i = 0; i < N; i++) {
btn[i] = new JButton();
btn[i].setBackground(Color.lightGray);
btn[i].addActionListener(new handlerClass()); // 为按钮添加ActionEvent事件
} Container frm = getContentPane(); // 初始化frm容器
frm.setLayout(new GridLayout(3, 3)); // 设置frm容器为BorderLayout布局(区域间隔10)
for (i = 0; i < N; i++)
frm.add(btn[i]);
frm.validate(); // 验证frm容器及其所有子组件 setSize(370, 180); // 设置窗口大小
setResizable(false); // 设置窗口为不可调整大小
setLocationRelativeTo(null); // 设置窗口位于屏幕中央
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置在窗口上发起 "close"
// 时默认执行的操作为退出应用程序
setVisible(true); // 设置窗口可见
} class handlerClass extends JFrame implements ActionListener {
public void actionPerformed(ActionEvent ee) {
int j;
for (j = 0; j < N; j++) {
if (ee.getSource() == btn[j]) {
if (x == j) {
btn[j].setBackground(Color.black);
count = 0; // 加入哭脸 cryFace frame1 = new cryFace();
frame1.run();
Thread t1=new Thread(frame1);
t1.start();
System.out.println("开始:"+t1.isAlive());
try {
Thread.sleep(3000);
System.out.println("睡眠:"+t1.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
frame1.shutDown();
System.out.println("结束:"+t1.isAlive());
// 哭脸结束
n = JOptionPane.showConfirmDialog(this,
"BOOooM!!!Would you like to play again?", " ",
JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) {
for (j = 0; j < N; j++)
btn[j].setBackground(Color.lightGray);
x = rnd.nextInt(9);
} else
System.exit(0);
} else {
btn[j].setBackground(Color.white);
count++;
}
if (count == 8) {
// 加入笑脸
smileface frame2 = new smileface();
Thread t2=new Thread(frame2);
t2.start();
frame2.run();
System.out.println("开始:"+t2.isAlive());
try {
Thread.sleep(3000);
System.out.println("睡眠:"+t2.isAlive());
} catch (InterruptedException e) {
e.printStackTrace();
}
frame2.shutDown();
System.out.println("结束:"+t2.isAlive());
n = JOptionPane.showConfirmDialog(this,
"You win!!!Would you like to play again?", " ",
JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) { for (j = 0; j < N; j++)
btn[j].setBackground(Color.lightGray);
x = rnd.nextInt(9);
} else
System.exit(0);
}
}
}
} }}public class MiniMineSweeper {
public static void main(String[] args) {
Sweeper sweep = new Sweeper("Boom!"); // 初始化sweep(Sweeper方法) }
}// 哭脸的
class cryFace extends JFrame implements Runnable{
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0, 0, 370, 180); g.setColor(Color.red);
g.fillOval(111, 30, 23, 23);
g.fillOval(222, 30, 23, 23);
g.drawArc(115, 80, 121, 90, 10, 160);
}
public void run() {
setTitle("Boom!");
setSize(370,180);
setLocationRelativeTo(null);
setVisible(true);
}
public void shutDown(){
this.dispose();
}
}
//笑脸
class smileface extends JFrame implements Runnable{
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillRect(0, 0, 370, 180);
g.setColor(Color.red);
g.fillOval(111, 30, 23, 23);
g.fillOval(222, 30, 23, 23);
g.drawArc(115, 25, 121, 90, 200, 145);
}
public void run() {
setTitle("Boom!");
setSize(370, 180);
setLocationRelativeTo(null);
setVisible(true);
}
public void shutDown(){
this.dispose();
}}
这个程序问题出在,t.start()进程却看不到笑、哭图象,但是整个程序不报告错误?? 请高手帮忙修正一下,小弟先谢谢了? ?/
Thread t2=new Thread(frame2);
t2.start();
frame2.run();好奇怪的写法。我根本看不懂你要干什么?
一个listener而已,干嘛要继承JFrame?
public void run() {
setTitle("Boom!");
setSize(370,180);
setLocationRelativeTo(null);
setVisible(true);
}
你的run方法这样实现有什么意义啊,这些东西应该再构造方法中
run方法里要做的一般是些循环的动作,比如你的应该是调用repaint();
线程里我不可能去执行方法
不好意思,
二楼说的,是因为n = JOptionPane.showConfirmDialog(this,
"BOOooM!!!Would you like to play again?", " ",
JOptionPane.YES_NO_OPTION); 是JFrame中的组件
再则class smileface和class cryFace这两个类是要附加在哪个JFrame上的啊?
感觉代码逻辑很混乱!