2015-10-18 68 views
1

我有2個擴展Jframe的類。我想在點擊按鈕時調用第二個Jframe,並在點擊關閉時返回。因此,在1日的JFrame我寫了這個代碼使用2個jframe swing Swing Jframe

public void actionPerformed(ActionEvent e) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
        Lessons frameLessons = new Lessons(); 
        frameLessons.setVisible(true); 
       } catch (Exception e) { 
         e.printStackTrace(); 
       } 
      } 
     }); 
    } 

但是,當我嘗試關閉第二JFrame中,第1也關閉

+2

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

+0

*「我有2類擴展Jframe。」*這是IDE通常鼓勵的東西(擴展GUI類而不是配置標準的實例)應該避免。話雖如此,如果決定繼續使用GUI構建器,最好是擴展'JPanel'。這樣,所有的面板都可以用在像框架這樣的單個頂層容器中 - 如果這是合適的,或者在對話框或小程序中顯示,或者...... *「當點擊按鈕時調用第二個Jframe,並在關閉時返回點擊。「*這表明第二個容器應該是一個**模式'JDialog' ** .. –

+0

謝謝,我解決 –

回答

-1

試試這個:

Lessons frameLessons = new Lessons(); 
frameLessons.setDefaultCloseOperation(DISPOSE_ON_CLOSE); 
frameLessons.setVisible(true); 
+1

雖然這將解決規定的問題,它也支持*設計*(多個幀)應該改變。 –