JFrame执行问题怎样在视窗完全显示后才执行其他程序?现在好像所有都执行了后视窗才显示出来
这是用netbeans
/*
 * NewJFrame.java
 */package javaapplication1;/**
 *
 * @author Administrator
 */
public class NewJFrame extends javax.swing.JFrame {    /** Creates new form NewJFrame */
    public NewJFrame() {
        initComponents();
    }    public boolean startProcess(){
        for(int i=0; i<10000; i++){
        jTextArea1.append(i+"\n");
        }
        return true;
    }    public void processTwo(){
        jTextArea1.append("start process two\n");
        //fake data list
        for(int i=0; i<10000; i++){
        jTextArea1.append("list file: a"+i+"\n");
        }
        processThree();
    }    public void processThree(){
        jTextArea1.append("start process three\n");
        //fake data list
        for(int i=0; i<10000; i++){
        jTextArea1.append("copy file from /source/"+i+" to /backup/"+i+"\n");
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);        jButton1.setText("Run");        jButton2.setText("Exit");        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(248, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton2)
                .addGap(28, 28, 28))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(66, 66, 66)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(69, Short.MAX_VALUE))
        );        pack();
    }// </editor-fold>    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               NewJFrame f = new NewJFrame();
               f.setVisible(true);               if(f.startProcess()){
                   f.processTwo();
               }
            }
        });
    }    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration}

解决方案 »

  1.   

    在WindowListener的windowOpened方法里调用
      

  2.   

    addWindowListener(new WindowAdapter(){
        public void windowOpened(){
            ... // 耗时的放到单独的线程里
         
        }
    });
      

  3.   


    //去掉构造方法
    NewJFrame f = new NewJFrame();           
    f.initComponents();
    f.setVisible(true);
    f.startProcess();
    f.processTwo();
    /*你就算使用了事件处理;结果也和我写的这样差不多
    你应该点击Run按钮 再运行方法效果才会明显*/