如何解决Java Swing 登录表单
我正在尝试使用 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 {
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);
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,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));
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);
}
}
主要
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);
}
}
这就是我想要的样子(更新):
REM(the title)
________________________________
__________________
Username: [__________________]
__________________
Password: [__________________]
[Submit button]
解决方法
我不清楚 submit 按钮是右对齐还是居中,但是..
- 蓝色区域:
BorderLayout
。 - 红色区域:
GridBagLayout
- 标签右对齐,文本字段左对齐。在边框布局的CENTER
中。 - 绿色区域:
FlowLayout
与PAGE_START
左对齐和PAGE_END
居中对齐或右对齐(根据需要)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。