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()进程却看不到笑、哭图象,但是整个程序不报告错误?? 请高手帮忙修正一下,小弟先谢谢了? ?/

解决方案 »

  1.   

    这个程序的入口是public class MiniMineSweeper {
        public static void main(String[] args) {
          Sweeper sweep = new Sweeper("Boom!"); // 初始化sweep(Sweeper方法)    }
    }在这里声明了一个sweeper并初始化,初始化的时候,会执行sweeper类的构造函数,它的构造函数是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); // 设置窗口可见
    }执行完这一段之后就会停下来,所以,自然不会启动线程了。
      

  2.   

    我只是给你分析原因而已。
    其实你的程序太乱了,如果你是初学的话可以看看《Java大学教程》,讲得很详尽,而且可以从里头学习
    编程习惯。