Java GUI,多个框架 [英] Java GUI, Multiple Frames

查看:138
本文介绍了Java GUI,多个框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建我在下面描述的内容?

How do I go about creating what I describe below?

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

First, here is the basic look of my GUI:

当我点击添加新帐户时,我希望GUI弹出一个小窗口,用户可以在其中输入登录凭据。我需要将这些信息传递回主GUI,所以我对如何解决这个问题感到迷茫。

When I click on Add New Account I want to have the GUI pop up a small window where the user can enter log-in credentials. I would need this information to be passed back into the main GUI, so I am lost as how to approach this.

偏好设置也是如此删除帐户。我如何创建各种GUI叠加。对不起,我无法弄清楚我正在寻找的效果的正确术语。

The same goes for Preferences or Remove Account. How do I go about creating a "GUI Overlay" of sorts. Sorry, I can't figure out the correct terminology for the effect I am looking for.

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

I wanted to attempt to use JOptionPane's, but after some research this seemed like it was not the route to be taking.

我还想要在执行操作时创建一个新的 JFrame 。该如何处理?

I was also toying with the idea of creating a new JFrame when the action was preformed. How should this be approached?

推荐答案

首先使用框架上的对话框。对话框用于从用户收集小块信息。

Start by using dialogs over frames. Dialogs are designed to gather small pieces of information from the user.

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

I would create a separate component for each operation you want to perform. Within these components I would provide setters and getters to allow you to gain access to the information managed by the component.

从那里我将使用 JOptionPane JDialog 将组件显示给用户。对我来说使用one over the other的原因归结为开始能够控制动作按钮( Okay 取消例如)。对于像登录对话框这样的东西,我想限制用户开始能够点击登录按钮,直到他们提供了足够的信息来进行尝试。

From there I would either use a JOptionPane or JDialog to display the component to the user. The reason for using one over the other for me comes down to begin able to control the action buttons (Okay and Cancel for example). For something like the login dialog, I want to restrict the user from begin able to hit the Login button until they've provided enough information to make the attempt.

基本跟随将是这样的......

The basic follow would be something like this...

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 可能看起来像这样...

The LoginDialog might look something like this...

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();
    }
}

该对话框只是充当代理/容器登录窗格。

The dialog is simply acting as proxy/container for the login pane.

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

This is, of course an overview, you will need to fill in the blanks ;)

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

Now, if you don't want to go to the trouble of creating your own dialog, you can take advantage of the JOptionPane instead.

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...
}

这篇关于Java GUI,多个框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆