有没有这样类似的线程控件:1.应用于java awt/swing上的
2.当线程运行中,最好能出现一个Dialog浮在主界面上,让所有的界面元素都不可操作
3.当后台抛错,线程主动停止,浮在主界面上的Dialog消失
4.浮在主界面上的Dialog最好有点进程条这样的表示效果

解决方案 »

  1.   

    1、所有的Dialog、Window、Frame都继承一次(重写setVisible(boolean)方法,参数为true时将控件自身加到一个静态变量中,为false时删除
    2、线程运行时,循环此变量,根据控件类型决定是否显示或将所有控件disable
      

  2.   

    这是我刚刚做的一个组建,有了进度条,你自己再添加一个窗口浮动就行了
    你在使用时,如果在主程序中捕获异常,只需在catch语句中调用shutDown()方法就可以停止线程,并关闭对话框。
    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(){
          while(isRun){
           try{
           Thread.sleep(second*1000);       //
           dialog.ctrlBar();     
           }catch(Exception e){
          
           }
          
          }
       
           
       
       }
       
       public void  shutDown(){
          isRun=false;
          dialog.close();
       }
    }
    class FlyDialog extends JDialog{
    JProgressBar bar=new JProgressBar(0,100);
    int i=0;
    FlyThread thread;
    public FlyDialog(JFrame frame){
    super(frame,true);
    JPanel p=(JPanel)this.getContentPane();
    p.setLayout(new BorderLayout());
    p.add(bar,BorderLayout.CENTER);

    this.addWindowListener(
    new WindowAdapter(){
    public void windowClosing(WindowEvent e){
        setVisible(false);
        thread.shutDown();
    dispose();
    }
    }

        );
        this.setVisible(true);
    this.setSize(300,150);
    this.validate();
    }

    public void ctrlBar(){

    if(++i>100){
    i=0;
    }

    bar.setValue(i);
    }

    public void close(){
    thread.shutDown();
    this.setVisible(false);
    this.dispose();

    }
    }
      

  3.   

    用个dialog不行了吗,设置modal为true
      

  4.   

    界面部分.JWindow里添加一个JProgressBar逻辑部分,维护一个线程个数,线程开始运行时,+1,判断如果是第一个,就将界面显示出来。等线程运行结束,计数-1.如果减一后为0.则隐藏界面//手写代码,未测试public class XThread extends Thread{    private static JWindow window =...;
        private static int threads = 0;
        private static Object lock = new Object();    public XThread(Runnable r){
           super(r);
        }
        
        public void run(){
           syn(lock){
              threads++;
              if(threads==1)
                  window.setVisible(true);
           }
           try{
              super.run();
           }
           finally{
              syn(lock){
              threads--;
              if(threads==0)
                  window.setVisible(false);
              }
           }
        }
    }
      

  5.   

    后台的操作是位于另一个线程中,因为前台UI线程在显示Dialog时仍是Active的