现在在做一个swing的画面
有一个按钮是输出文件,点下之后
输出时间很长,这时候在原有的画面上
重绘一个小的panel可以么?
输出完毕后,再把小的panel隐藏?
具体怎么做,麻烦各位了

解决方案 »

  1.   

    开辟新线程,将耗时操作放到线程里完成.
    在主线程里爱干嘛干嘛,同时增加一个线程监控,当耗时操作完成时触发该方法将主界面刷新.
    over
      

  2.   

    给你一个例子
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;import javax.swing.JFrame;public class Test extends JFrame {  /**
       * Launch the application
       * @param args
       */
      public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
          public void run() {
            try {
              Test frame = new Test();
              frame.setVisible(true);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
      }  /**
       * Create the frame
       */
      public Test() {
        super();
        getContentPane().setLayout(null);
        setBounds(100, 100, 500, 375);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    final JButton button = new JButton();
        button.addActionListener(new ActionListener() {
          public void actionPerformed(final ActionEvent e) {
            
            Thread thread = new Thread(new Runnable() {          public void init() {
                // 初始化界面,比如说画一个panel
              }
              
              public void update() {
                //更新界面,比如销毁panel
              }
              
              public void run() {
                EventQueue.invokeLater(new Runnable() {
                  public void run() {
                    init();
                  }
                });
                
                //..........
                // 执行输出文件的操作
                //.........
                
                EventQueue.invokeLater(new Runnable() {
                  public void run() {
                    update();
                  }
                });
                
              }
            });
            
            thread.start();// 启动线程
          }
        });
        button.setText("New JButton");
        button.setBounds(163, 137, 106, 26);
        getContentPane().add(button);
        //
      }}
      

  3.   

    事件派发线程Swingutility.invokeLater();
      

  4.   

    用Swingutility.invokeLater()搞定了。
    谢谢各位