jwzhang0404
发贴: 14
积分: 0
|
于 2006-06-16 13:46
问一个问题,我在主程序里开了一个线程,启动线程,在主程序里检查线程终止的条件,可是发现弹出的对话框的布局不能正确显示,因为我在对话框上显示了一个label显示不出来。 我在另一个工程里调试,发现线程结束了,应该主程序也该结束了,却发现主程序没有退出,不知为什么。我把我调试时的代码,发出来让大家看看哈,请各位高手帮忙。谢先。 import java.awt.event.*; import javax.swing.*; import java.awt.*;
public class FlyThread extends Thread {
private JFrame frame; private int second; private FlyDialog dialog; private boolean isRun;
public FlyThread(JFrame frame, int s) { this.frame = frame; second = s; dialog = new FlyDialog(frame); isRun = true;
}
public void run() { int i = 1; while (isRun) { try { //dialog.ctrlBar(); this.dialog.setVisible(true); Thread.sleep(second * 1000); // System.out.println("times" + i++); } catch (Exception e) {}
}
}
public void shutDown() { isRun = false; this.dialog.close(); //this.destroy(); //this.stop(); } //线程关闭了主程序为什么不退出? public static void main(String[] args) { try{ JFrame frame = new JFrame(); FlyThread thread = new FlyThread(frame,10); thread.start(); Thread.sleep(30000); thread.shutDown(); } catch(Exception e) {} } }
class FlyDialog extends JDialog { int i = 0; FlyThread thread;
public FlyDialog(JFrame frame) { super(frame, true); JPanel p = (JPanel) this.getContentPane(); JLabel label = new JLabel("正在进行恢复数据,请等恢复完成再进行其它操作。"); p.setLayout(new BorderLayout()); p.add(label,BorderLayout.CENTER);
/*this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { setVisible(false); thread.shutDown(); dispose(); } }
*/ //this.setVisible(true); this.setSize(300, 180); this.setResizable(false); this.setUndecorated(true); this.validate(); }
public void close() { //thread.shutDown(); this.setVisible(false); this.dispose();
} }
|