2012-12-03 108 views
0

如何創建我在下面描述的內容?Java GUI多框架

首先,這裏是我的GUI的基本外觀:

enter image description here

當我點擊Add New Account我想有GUI彈出一個小窗口,在這裏用戶可以輸入登錄憑證。我需要將這些信息傳遞迴主GUI,所以我迷失了方法。

PreferencesRemove Account也是如此。我該如何着手創建一個「GUI Overlay」類。對不起,我無法弄清楚我正在尋找的效果的正確術語。

我想嘗試使用JOptionPane's,但經過一些研究後,這似乎並不是要採取的路線。

我還在玩弄創建一個新的JFrame,當行動被執行的想法。這應該如何處理?

+1

不這樣做。看看這個問題:http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice您可以使用模式對話框。 –

+0

好的。我現在正在使用一個內部框架。但問題是,它試圖使用主窗格的佈局管理器來定位自己,即使我設置了位置和大小。 – Brendan

回答

3

通過使用幀對話框開始。對話框旨在收集用戶的小部分信息。

我會爲每個要執行的操作創建一個單獨的組件。在這些組件中,我會提供setter和getters來讓您訪問組件管理的信息。我想用JOptionPaneJDialog向用戶顯示組件。爲我使用一個的原因歸結爲能夠控制動作按鈕(例如,OkayCancel)。對於類似於登錄對話框的內容,我想限制用戶在能夠提供足夠的信息進行嘗試之前就能夠開始按Login按鈕。

基本後續會是這樣的......

LoginDialog dialog = new LoginDialog(SwingUtilities.getWindowAncestor(this)); // this is a reference any valid Component 
dialog.setModal(true); // I would have already done this internally to the LoginDialog class... 
dialog.setVisible(true); // A modal dialog will block at this point until the window is closed 
if (dialog.isSuccessfulLogin()) { 
    login = dialog.getLogin(); // Login is a simple class containing the login information... 
} 

LoginDialog可能是這個樣子......

public class LoginDialog extends JDialog { 
    private LoginPanel loginPane; 
    public LoginDialog(Window wnd) { 
     super(wnd); 
     setModal(true); 
     loginPane = new LoginPanel(); 
     setLayout(new BorderLayout()); 
     add(loginPane); 
     // Typically, I create another panel and add the buttons I want to use to it. 
     // These buttons would call dispose once they've completed there work 
    } 

    public Login getLogin() { 
     return loginPane.getLogin(); 
    } 

    public boolean isSuccessfulLogin() { 
     return loginPane.isSuccessfulLogin(); 
    } 
} 

的對話框只是充當代理/容器登錄窗格。

這是當然的概述,您將需要填補空白;)

現在,如果你不想去創建自己的對話框的麻煩,可以利用改爲JOptionPane

LoginPanel loginPane = new LoginPanel(); 
int option = JOptionPane.showOptionDialog(
    this, // A reference to the parent component 
    loginPane, 
    "Login", // Title 
    JOptionPane.YES_NO_OPTION, 
    JOptionPane.QUESTION_MESSAGE, 
    null, // You can supply your own icon it if you want 
    new Object[]{"Login", "Cancel"}, // The available options to the user 
    "Login" // The "initial" option 
    ); 
if (option == 0) { 
    // Attempt login... 
} 
+0

這應該解決我在內部框架中看到的整體問題;但我仍然對如何創建對話框感到困惑。你顯示一個'私人LoginPanel loginPane;'沒有引用什麼'LoginPanel'。它只是一個擴展'JPanel'的類嗎?包含我將要添加的各種組件? – Brendan

+0

是的'LoginPane'將會延伸到像'JPanel'這樣的東西,幷包含所需的UI組件。然後它會根據需要獲得getter和setter – MadProgrammer