Java Swing登录表单 [英] Java Swing Login Form

查看:129
本文介绍了Java Swing登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java Swing创建登录表单.但我希望布局看起来更紧凑,但仍能响应.

我的项目的结构

 项目/└──src├──GUI│├──Control.java│──├──Main.java│└──页面│├──LoginPanel.java└──帮手└──Helpers.java 

编辑

尝试了答案后,我得到了:

我不清楚 submit 按钮是否正确对齐或居中,但是..

  • 蓝色区域: BorderLayout .
  • 红色区域: GridBagLayout -标签右对齐,文本字段左对齐.在边框布局的 CENTER 中.
  • GREEN区域:与 PAGE_START 左对齐的 FlowLayout 和& PAGE_END 居中对齐或右对齐(根据需要).

I'm trying to create a login form using Java Swing. But I want the layout to look tighter, but still responsive.

The structure of my project

Project/
└── src
    ├── GUI
    │   ├── Control.java
    │   ├── Main.java
    │   └── pages
    │       ├── LoginPanel.java
    └── Helper
        └── Helpers.java

Edit

After trying out the answer posted, I got:

This is the picture

The components in the form panel are not behaving as I would expect

And my code:

LoginPanel

package GUI.pages;
import javax.swing.*;

public class LoginPanel extends JPanel {

    private static final int PADDING = 30;
    // Some labels, button, groups and fields
    private static JLabel usernameLabel;
    private static JTextField usernameInputField;
    private static JLabel passwordLabel;
    private static JPasswordField passwordInputField;
    private static JButton submit;
    private static JLabel title = Helpers.generateTitle();


    public LoginPanel() {

        setLayout(new BorderLayout());
        setSize(400, 400);

        JPanel titleContainer = new JPanel();
        titleContainer.setLayout(new FlowLayout(FlowLayout.LEFT, PADDING, PADDING));
        titleContainer.add(title);

        // the username row
        usernameLabel = new JLabel("Username: ");
        usernameInputField = new JTextField(50);

        // The password row
        passwordLabel = new JLabel("Password: ");
        passwordInputField = new JPasswordField(150);

        JPanel form = new JPanel();
        form.setLayout(new GridBagLayout());

        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(0,20,0,0);
        form.add(usernameLabel, c);

        c = new GridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        c.anchor = GridBagConstraints.WEST;
        c.insets = new Insets(0,20,0,0);
        form.add(passwordLabel, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 0;
        c.weightx = 0.1;
        c.ipadx = 200;

        form.add(usernameInputField, c);

        c = new GridBagConstraints();
        c.gridx = 1;
        c.gridy = 1;
        c.weightx = 0.001;
        c.weightx = 0.001;
        c.ipadx = 200;

        form.add(passwordInputField, c);

        // The message label, the text depends on the query's result.
        message = new JLabel();

        // Configuring the submit button
        submit = new JButton("Sign in");
        submit.addActionListener(new LoginActionListener());
        submit.setActionCommand("login");

        // Configuring the redirecting button
        registerRedirect.setActionCommand("registerRedirect");
        registerRedirect.addActionListener(new Router());


        JPanel submitContainer = new JPanel();
        submitContainer.setLayout(new FlowLayout(FlowLayout.RIGHT, PADDING, PADDING));
        submitContainer.add(submit);

        form.setMaximumSize(new Dimension(200, passwordInputField.getHeight()));

        // adding everything to the layout

        add(titleContainer, BorderLayout.PAGE_START);
        add(form);
        add(submitContainer, BorderLayout.PAGE_END);

    }
}

Main

package GUI;

import javax.swing.*;
import GUI.pages.LoginPanel;

public class Main extends JFrame {

    public final static int WINDOW_WIDTH = 750;
    public final static int WINDOW_HEIGHT = 550;

    public static void main(String[] args){
        JFrame frame = new Main();
    }

    public Main(){
        JPanel login_panel = new LoginPanel();
        add(login_panel);
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

}

This is how I want it to look like(updated):

REM(the title)        
________________________________
           __________________
Username: [__________________]
           __________________
Password: [__________________]
                      
                   [Submit button]
                    

解决方案

I'm not clear if the submit button is right aligned or centered, but..

  • BLUE area: BorderLayout.
  • RED area: GridBagLayout - with labels right aligned, and text fields left aligned. In the CENTER of the border layout.
  • GREEN areas: FlowLayout with PAGE_START left aligned & PAGE_END either center aligned or right aligned (as per need).

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

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