2014-04-09 28 views
-1

Salah問我這裏的代碼的其他部分是。我希望你能看到它 。有些人抱怨,因爲我發送了第一個未格式化的代碼。 非常感謝您的幫助。Java GUI數學測試

下面是MathTest代碼的主要方法。 它創建一個MathTest對象並將其顯示在JFrame 一旦第一個JFrame/MathTest對象關閉,我想 爲下一個學生顯示它的新版本。 我怎麼能一個接一個地顯示兩個JFrame/MathTest對象?

import javax.swing.JFrame; 

public class University 
{ 
    public static void main (String [] args) //main method 
    { 
     MathTest mt= new MathTest(); //Create MathTest object 

     mt.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Put in JFrame 
     mt.setSize(500, 500); // Size JFrame 
     mt.setVisible(true);  // Display 

    }//end main 
}// end Uni 
+0

你可能應該編輯你正在談論的任何問題,並將其添加到那裏。 –

+0

除非我錯了,我相信這是你的其他問題的重複:[Java Gui程序](http://stackoverflow.com/questions/22972660/java-gui-program)。如果是這樣的話,就像Mike B說的那樣,最好是編輯這個問題並更新其內容,而不是發佈一個新問題。 –

+0

而不是重新顯示框架,爲什麼不重新設置內容?這樣,一旦測試完成,您可以保存結果並將框架重置回「開始」,清除以前的答案。一個模型將是一個更好的解決方案來實現這一點,因爲它將數據從視圖中分離出來,並且只需要您用新模型替換現有模型... – MadProgrammer

回答

0

這聽起來像你可能想使MathTest延長JDialog而不是JFrame。那麼你可以mt.setModel(true);,這將阻止代碼,直到它關閉。這將允許你這樣做:

while (exitConditionNotMet) { 
     MathTest mt = new MathTest(); 
     mt.setModel(true); 
     // other stuff 
     mt.setVisible(true); 

     checkForExitCondition(); 
    } 
+0

此消息針對澳大利亞MadProgramer:判斷您的證書,也許你可以幫我解決一個我用我的gui問題。你有興趣通過電子郵件直接諮詢我嗎?由於我通過這個stackoverflow網站遇到了麻煩,因此我更容易直接與程序員打交道。 –

0

你可以嘗試製作自己的關閉方法,而不是使用JFrame.EXIT_ON_CLOSE 並在close方法創建一個新的MathTest對象,然後重新設置的東西,然後秀它恰好在你關閉第一個之前(這隻有在你需要2個數學計算或者你將創建Mathtest對象的無限循環時纔會起作用,這將需要你強制退出程序才能真正離開)

或者,包含Mathtest對象的數組,每次關閉數組時,顯示下一個數組,如果到達數組的末尾,只需使用標準關閉操作,以便主要方法可以用exi噸。

0

要做到這一點,你需要改變關閉框架JFrame.DO_NOTHING_ON_CLOSE的動作,並添加一個WindowListener(我用的是WindowAdapter,因爲它使事情變得更容易),其編程關閉當前幀,並打開一個新的(我只是跟着在關閉窗口的回答中提供了this的建議)。

public static void main(String[] args){ 
    //this removes the creation of the frame into a new 
    //method that allows us to more easily make the mt 
    //variable final, which is important 
    createMathTest(); 

    //also, it's technically a bad practice to initiate Swing 
    //components outside of the correct Swing thread. 
    //I would recommend doing something along the lines of: 
    //SwingUtilities.invokeLater(new Runnable() { 
    // @Override 
    // public void run() { 
    //  createMathTest(); 
    // } 
    //} 
} 

public static void createMathTest() { 
    //we need this to be final 
    //because you can only access variables declared final 
    //in anonymous classes 
    final MathTest mt= new MathTest(); //Create MathTest object 

    //add in a close handler 
    mt.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 
    mt.addWindowListner(new WindowAdapter() { 
     @Override 
     public void windowClosed(WindowEvent e) { 
      //hide the window 
      mt.setVisible(false); 
      //dispose our window 
      mt.dispose(); 
      //make a new test and show it! 
      createMathTest(); 
     } 
    }); 

    mt.setSize(500, 500); // Size JFrame 
    mt.setVisible(true);  // Display 
} 

雖然,作爲一個previous答覆中提到,這導致無限數量的接連出現的測試,這意味着關閉您可能需要放置一個控制與功能,實際上退出程序該程序,或通過您的操作系統的機制強行關閉它。