通過使用幀對話框開始。對話框旨在收集用戶的小部分信息。
我會爲每個要執行的操作創建一個單獨的組件。在這些組件中,我會提供setter和getters來讓您訪問組件管理的信息。我想用JOptionPane
或JDialog
向用戶顯示組件。爲我使用一個的原因歸結爲能夠控制動作按鈕(例如,Okay
和Cancel
)。對於類似於登錄對話框的內容,我想限制用戶在能夠提供足夠的信息進行嘗試之前就能夠開始按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...
}
不這樣做。看看這個問題:http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice您可以使用模式對話框。 –
好的。我現在正在使用一個內部框架。但問題是,它試圖使用主窗格的佈局管理器來定位自己,即使我設置了位置和大小。 – Brendan