2016-05-17 27 views
0

我有第一個框架包含一個按鈕。按下按鈕,我調用不同類的actionPerformed()方法。Java Swing:在不同類的ActionPerformed()中創建框架

JButton compress = new JButton("Submit"); 
compress.addActionListener(new Action1(inp,out,frame1)); // inp,out are textboxes and frame1 is 1st frame containing textboxes and JButton 

在actionPerformed()類的Action1中。我已創建另一幀有如下

static class Action1 implements ActionListener {   

     JTextField input_path,out_path; 
     JFrame prev; 

     public Action1(JTextField inp,JTextField out,JFrame jf) 
     { 
      input_path = inp; 
      out_path = out; 
      prev = jf; 
     } 

     public void actionPerformed (ActionEvent e) {   
      prev.dispose(); 
      try{ 
       drawFrame(); 
       // launch the compression job 
       launchJob(input_path.getText(),out_path.getText()); 
      } 
      catch(IOException io){ 
       io.printStackTrace(); 
      } 

     } 
     public void drawFrame() 
     { 
      JFrame frame2 = new JFrame("New Frame"); 
      JPanel panel = new JPanel(); 

      frame2.setSize(400,300); 
      frame2.setLocation(500, 300); 
      JLabel label = new JLabel(" in Progress..."); 

      panel.add(label); 
      frame2.add(panel); 
      frame2.setVisible(true); 
     } 
    } 

但在的actionPerformed()時,幀2的內容物的方法launchJob之後得到可見()被執行。我想在我的函數launchJob()開始執行之前顯示(顯示)frame2的內容。你能否建議我哪裏出錯或有其他選擇。謝謝。

+0

請參閱[使用多個JFrames,好/壞實踐?](http://stackoverflow.com/q/9554636/418556) –

回答

0

我剛剛證實您的框架正在設置可見之前您的方法launchJob()被調用。

考慮增加一些打印語句來調試,例如,在並條機()方法的末尾,添加以下代碼:

frame2.setVisible(true); 
System.out.println("frame visible"); 

做同樣在推出工作方法的開始:

System.out.println("doing launch job"); 

你會看到輸出是:

frame visible 
doing launch job 

這驗證了JFrame 實際上是在啓動作業方法之前可見的

+0

JFrame正在變得可見,但內容(面板,標籤「進行中」)在launchJob()方法完成之後,Jframe是可見的...可以告訴我abt那個plz ... – Sumit

+0

爲了更好地幫助,請發佈[MCVE]或[Short,Self Contained,Correct Example](http:// www.sscce.org/)。 –

+0

@Sumit:請參閱[這裏](http://stackoverflow.com/q/20901770/2711488),尤其是對第4點的回答。 – Holger