[英]Java Swing `GridBagLayout` alignment issue
我正在嘗試使用 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]
我希望這些字段靠近標簽,但不水平填充整個空間。
public final static int WINDOW_WIDTH = 750;
public final static int WINDOW_HEIGHT = 550;
不要嘗試設置 window 大小。 所有 Swing 組件將確定自己的首選尺寸。 在調用 setVisible(...) 方法之前,將所有組件添加到框架和pack()
框架。 然后將根據所有組件的首選大小來調整框架的大小。
private static JLabel usernameLabel;
private static JTextField usernameInputField;
不要使用 static 變量。 這不是應該使用 static 關鍵字的方式。
無需為您的標簽創建實例變量。 當您希望變量在多個方法中被引用時,您通常只創建實例變量。 label 僅用於在表格上顯示。 文本字段將在按鈕的 ActionListener 中引用,因此它們應該是實例變量。
setSize(400, 400);
不要嘗試設置大小。 額外的垂直高度是因為您只使用 400 作為框架的隨機高度。 pack() 方法將確定首選大小。
usernameInputField = new JTextField(50);
...
passwordInputField = new JPasswordField(150);
構造函數中的數字將用於調整文本字段的大小以包含“W”字符。 它不是像素。 所以你指定的值太大了。
c.fill = GridBagConstraints.HORIZONTAL;
不需要“填充”約束。
form.setMaximumSize(new Dimension(200, passwordInputField.getHeight()));
無需指定最大尺寸。
我還建議您可以使用GridBagLayout
完成整個布局。
對於第一行中的 label,您將使用gridwidth = 2
。 最后一行的按鈕也是如此。
然后可以使用anchor
約束來控制行上標簽/按鈕的alignment。 也就是說,您是否希望 label 居中且按鈕右對齊。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.