【发布时间】:2021-06-21 01:55:46
【问题描述】:
我正在尝试使用 Java Swing 创建登录表单。但我希望布局看起来更紧凑,但仍然响应迅速。
我的项目结构
Project/
└── src
├── GUI
│ ├── Control.java
│ ├── Main.java
│ └── pages
│ ├── LoginPanel.java
└── Helper
└── Helpers.java
form 面板中的组件的行为与我预期的不同,我希望字段靠近标签,但不水平填充整个空间。
还有我的代码:
登录面板
package GUI.pages;
import javax.swing.*;
public class LoginPanel extends JPanel {
// Some labels, button, groups and fields
private static JTextField usernameInputField;
private static JPasswordField passwordInputField;
private static JButton submit;
private static JLabel message;
private static JButton registerRedirect = new JButton("register");
private static final int PADDING = 30;
public LoginPanel() {
setLayout(new BorderLayout());
JLabel title = Helpers.generateTitle();
JPanel titleContainer = new JPanel();
titleContainer.setLayout(new FlowLayout(FlowLayout.LEFT, PADDING, PADDING));
titleContainer.add(title);
// the username row
JLabel usernameLabel = new JLabel("Username: ");
usernameInputField = new JTextField(50);
// The password row
JLabel passwordLabel = new JLabel("Password: ");
passwordInputField = new JPasswordField(50);
JPanel form = new JPanel();
form.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = 2;
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.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;
JPanel usernameFieldContainer = new JPanel();
usernameFieldContainer.add(usernameInputField);
usernameFieldContainer.setSize(new Dimension(usernameInputField.getWidth() + 10, usernameInputField.getHeight() + 10));
form.add(usernameFieldContainer, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridy = 1;
c.weightx = 0.001;
c.weightx = 0.001;
c.ipadx = 50;
JPanel passwordFieldContainer = new JPanel();
passwordFieldContainer.add(passwordInputField);
passwordFieldContainer.setSize(new Dimension(passwordInputField.getWidth() + 10, passwordInputField.getHeight() + 10));
form.add(passwordFieldContainer, c);
// Configuring the submit button
submit = new JButton("Sign in");
submit.addActionListener(new LoginActionListener());
submit.setActionCommand("login");
JPanel submitContainer = new JPanel();
submitContainer.setLayout(new FlowLayout(FlowLayout.RIGHT, PADDING, PADDING));
submitContainer.add(submit);
// adding everything to the layout
add(titleContainer, BorderLayout.PAGE_START);
add(form, BorderLayout.LINE_START);
add(submitContainer, BorderLayout.PAGE_END);
}
}
主要
package GUI;
import javax.swing.*;
import GUI.pages.LoginPanel;
public class Main extends JFrame {
public static void main(String[] args){
JFrame frame = new Main();
}
public Main(){
JPanel login_panel = new LoginPanel();
add(login_panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}
}
这就是我想要的样子(更新):
REM(the title)
________________________________
__________________
Username: [__________________]
__________________
Password: [__________________]
[Submit button]
【问题讨论】:
-
如果你想加入这个社区,你必须遵守规则。当你上次问这个问题时,我们都可以看到你是如何对待安德鲁的。 Andrew's answer 有什么问题?哦,顺便说一句,登录面板最好放在 JDialog 上。 Swing 教程,How to Use GridBagLayout 可能会有所帮助。
-
我是新人,不是回答/提问方面的专家。所以,对不起我的所作所为。 @Abra 它并没有解决我的问题,你可以在更新的图片上看到结果。
标签: java swing user-interface layout-manager gridbaglayout