2011-08-17 54 views
3

如何關閉Java GUI而不需要System.exit?當我使用處理() GUI關閉,但在此之後它不會重新啓動?我使用圖形用戶界面來自動抓取圖像並將其寫入磁盤,因此每次啓動GUI時都需要重複此操作。後來,我想連接這個GUI以在MATLAB中啓動。沒有System.exit的情況下關閉Java GUI

public class MainWindow { 
    int fs_c = 1; 
    MainWindow() { 
     JFrame main_f = new JFrame("GUI"); 
     main_f.getContentPane().setLayout(new BorderLayout()); 
     main_f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
     main_f.getContentPane().add(tabPane1, "Center"); 

     //main_f.setSize(500, 620); 
     main_f.pack(); 
     main_f.setVisible(true); 
     commandVal = 0; 

     while (true) { 
      if (fs_c == 1) { 
       commandVal = 5; 
      } 
      if (commandVal == 5 || commandVal == 6) { 
       cImage.sendFrame(0); 
       JFileChooser save_d = new JFileChooser(); 
       File saveFile = save_d.getSelectedFile(); 
       cImage.writeImage(saveFile + ".jpg"); 
       fs_c = 0; 
       main_f.dispose(); 
      } else if (commandVal == -1) { 
       stopCameraStuff(); 
      } 
     } 
    } 
} 

回答

3

你也應該打破while循環,以便該方法可以完成並返回給調用者。

+0

謝謝你的工作!現在我遇到了另一個問題,因爲每次GUI加載serial.dll時,MATLAB都會提示錯誤:「本機庫C:\ serial.dll已經加載到另一個類加載器中」。你能幫我解決嗎? – Makaroni

+0

@Makaroni:「Native Library whatever.dll已經加載到另一個類加載器中。」似乎已經在SO上多次回答:http://stackoverflow.com/questions/3724421/native-library-already-loaded-in-another-classloader有一個答案和其他幾個鏈接。 – millimoose

3

我會用:

setVisible(false); 

代替Dispose()方法。這樣你可以調用setVisible(true)使它重新出現。

+0

或者,您可以使用'setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);' – millimoose

+0

@Sii:是的 – dacwe

相關問題