javagui,多帧

javagui,多帧,java,swing,user-interface,jframe,overlay,Java,Swing,User Interface,Jframe,Overlay,我如何着手创建我下面描述的内容 首先,这里是我的GUI的基本外观: 当我单击添加新帐户时,我想让GUI弹出一个小窗口,用户可以在其中输入登录凭据。我需要将这些信息传递回主GUI,因此我不知道如何处理这些信息 首选项或删除帐户也是如此。我该如何创建一个“GUI覆盖”之类的东西呢。很抱歉,我找不出我想要的效果的正确术语 我想尝试使用JOptionPane,但经过一些研究后,这似乎不是我要走的路线 当动作执行时,我也在考虑创建一个新的JFrame。如何实现这一点?首先在框架上使用对话框。对话框设计用

我如何着手创建我下面描述的内容

首先,这里是我的GUI的基本外观:

当我单击添加新帐户时,我想让GUI弹出一个小窗口,用户可以在其中输入登录凭据。我需要将这些信息传递回主GUI,因此我不知道如何处理这些信息

首选项
删除帐户
也是如此。我该如何创建一个“GUI覆盖”之类的东西呢。很抱歉,我找不出我想要的效果的正确术语

我想尝试使用
JOptionPane
,但经过一些研究后,这似乎不是我要走的路线


当动作执行时,我也在考虑创建一个新的
JFrame
。如何实现这一点?

首先在框架上使用对话框。对话框设计用于从用户处收集小部分信息

我将为您要执行的每个操作创建一个单独的组件。在这些组件中,我将提供setter和getter,以允许您访问组件管理的信息

从那里,我可以使用
JOptionPane
JDialog
向用户显示组件。对我来说,使用一个而不是另一个的原因在于开始能够控制操作按钮(
ok
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...
}
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();
    }
}
LoginDialog
可能看起来像这样

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...
}
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();
    }
}
该对话框只是作为登录窗格的代理/容器

当然,这是一个概述,您需要填写以下空白;)

现在,如果您不想麻烦地创建自己的对话框,您可以利用


首先在框架上使用对话框。对话框设计用于从用户处收集小部分信息

我将为您要执行的每个操作创建一个单独的组件。在这些组件中,我将提供setter和getter,以允许您访问组件管理的信息

从那里,我可以使用
JOptionPane
JDialog
向用户显示组件。对我来说,使用一个而不是另一个的原因在于开始能够控制操作按钮(
ok
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...
}
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();
    }
}
LoginDialog
可能看起来像这样

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...
}
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();
    }
}
该对话框只是作为登录窗格的代理/容器

当然,这是一个概述,您需要填写以下空白;)

现在,如果您不想麻烦地创建自己的对话框,您可以利用


不要那样做。看看这个问题:你可以用模态对话框代替。我现在使用的是内部框架。但问题是,它试图使用主窗格的布局管理器来定位自己,即使我设置了位置和大小。不要这样做。看看这个问题:你可以用模态对话框代替。我现在使用的是内部框架。但问题是,它试图使用主窗格的布局管理器来定位自身,即使我设置了位置和大小;然而,我仍然对如何创建对话框感到困惑。您将显示一个
私有LoginPanel loginPane不引用什么是
LoginPanel
。它只是一个扩展了
JPanel
的类吗?包含我将要添加的各种组件?是的
LoginPane
将从
JPanel
扩展而来,并包含所需的UI组件。然后,它将有需要的getter和setter,这将解决我在内部框架中看到的总体问题;然而,我仍然对如何创建对话框感到困惑。您将显示一个
私有LoginPanel loginPane不引用什么是
LoginPanel
。它只是一个扩展了
JPanel
的类吗?包含我将要添加的各种组件?是的
LoginPane
将从
JPanel
扩展而来,并包含所需的UI组件。然后,它将根据需要使用getter和setter