简体   繁体   English

用于软件的Java Swing GUI,最好的方法

[英]Java Swing GUI for a software, best way to do it

I need to create a GUI for a program with a panel to login that will show the real software after the login is set. 我需要为一个程序创建一个GUI,其中有一个登录面板,它将在设置登录后显示真实的软件。

So the question is: 所以问题是:

Should i create two frames and show the first for login and the second for program? 我应该创建两个帧并显示第一个用于登录,第二个用于程序吗? Or is better to add two different panels and hide the login when the program starts? 或者最好添加两个不同的面板并在程序启动时隐藏登录?

This is a little subjective. 这有点主观。 For me, it would come down to how complex the application is and whether you would need to support multiple sessions and if those sessions shared a single frame or not. 对我来说,这将归结为应用程序的复杂程度以及是否需要支持多个会话以及这些会话是否共享单个帧。

If you application relative simple (has one or two views for example), I'd consider it acceptable use something like a CardLayout and simply show the login pane within the main application frame, when the user is successfully authenticated, switch to the main view. 如果您的应用程序相对简单(例如有一个或两个视图),我认为可以使用类似CardLayout东西,只需在主应用程序框架中显示登录窗格,当用户成功通过身份验证后,切换到主视图。

If the application is more complex, I'd consider using a separate dialog. 如果应用程序更复杂,我会考虑使用单独的对话框。

Even if you're allowing the user to have multiple sessions, it would come down to how complex the actual application is (IMHO) as to whether I would use a separate dialog or not. 即使您允许用户拥有多个会话,也可以归结为实际应用程序(IMHO)的复杂程度,以及我是否会使用单独的对话框。

I'd also consider if the user can do anything before they login, for example, can they update settings? 我还会考虑用户在登录前是否可以执行任何操作,例如,他们是否可以更新设置? If so, using a panel would be suitable as the login dialog is most likely going to be modal. 如果是这样,使用面板将是合适的,因为登录对话框很可能是模态的。

Personally I would create a jFrame for the actual program first, then (immediately) open a jDialog window and open it in front of your jFrame. 我个人首先为实际程序创建一个jFrame,然后(立即)打开一个jDialog窗口并在你的jFrame前面打开它。 The jDialog class has a handy property that when it's opened, you cannot perform actions on your other frame. jDialog类有一个方便的属性,当它打开时,你不能在你的另一帧上执行操作。 So it has to be closed (after some action, like filling in a password) first. 因此必须首先关闭(在执行某些操作之后,如填写密码)。

Using the GlassPane of the frame is an option (assuming you don't need the use of dropdowns). 使用框架的GlassPane是一个选项(假设您不需要使用下拉列表)。 Some people may argue that it's an abuse of the pane system, but if the login screen is simple enough, I think it's legitimate. 有些人可能会说这是滥用窗格系统,但如果登录屏幕足够简单,我认为这是合法的。

The benefit is that it's already there for you to use (It doesn't require significant changes to your application), and that it eats any events (so they don't get passed to the actual application). 好处是它已经存在供您使用(它不需要对您的应用程序进行重大更改),并且它会占用任何事件(因此它们不会传递给实际应用程序)。 It's also very easy to show/hide. 显示/隐藏也很容易。

The downfall is a few swing components don't work on the glass pane (JComboBox is an example). 垮台是一些摆动组件在玻璃窗格上不起作用(JComboBox就是一个例子)。

Here's an example of using the GlassPane: 以下是使用GlassPane的示例:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class QuickTest {

    public QuickTest(){
        //Setup Frame
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
        frame.pack();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);

        //Setup Main Content
        JPanel main = new JPanel();
        main.add(new JLabel("Here's the application!"));
        frame.setContentPane(main);

        //Setup login Screen
        Box login = new Box(BoxLayout.Y_AXIS){
            // Makes the main screen faded
            public void paintComponent(Graphics g){
                g.setColor(new Color(255,255,255,200));
                g.fillRect(0,0, getWidth(), getHeight());
                super.paintComponent(g);
            }
        };
        login.add(new JLabel("Username here:"));
        login.add(new JLabel("Password here:"));
        JButton loginButton = new JButton("login");
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.getGlassPane().setVisible(false);
            }
        });
        login.add(loginButton);
        login.add(Box.createVerticalGlue());
        frame.setGlassPane(login);
        frame.getGlassPane().setVisible(true);

        //Show Frame
        frame.setVisible(true);


    }

    public static void main(String[] args){
        new QuickTest();
    }
}

You do a page for login and check the user right with session enable. 您执行登录页面并使用会话启用检查用户权限。 If the user is identified then you can redirect to another page. 如果识别出用户,则可以重定向到另一个页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM