Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 求助,关于线程和对话框显示问题,各位请进!
jwzhang0404





发贴: 14
积分: 0
于 2006-06-16 13:46 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
问一个问题,我在主程序里开了一个线程,启动线程,在主程序里检查线程终止的条件,可是发现弹出的对话框的布局不能正确显示,因为我在对话框上显示了一个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();
      }
    }

    Wink;*/
    //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();

  }
}



作者 Re:求助,关于线程和对话框显示问题,各位请进! [Re:jwzhang0404]
jwzhang0404





发贴: 14
积分: 0
于 2006-06-22 10:12 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
没人顶啊,怎么,自己顶,不顶不行!


作者 Re:求助,关于线程和对话框显示问题,各位请进! [Re:jwzhang0404]
lisliefor





发贴: 287
积分: 7
于 2006-06-22 12:20 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这个帖子是不是贴了很多地方,我好像在某个地方回复了!



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923