gpt4 book ai didi

java - 使用 GridBagLayout 调整大小时防止组件散开

转载 作者:行者123 更新时间:2023-11-29 05:23:22 25 4
gpt4 key购买 nike

编辑:每次我尝试添加 gui 标签时,它都会切换到 user-interface。有人介意解释/解决这个问题吗?

我希望客户端可以调整大小。我希望 JSeparator 在调整大小时填充框架的宽度,但我希望 JLabel 保持在字段旁边。

一开始是这样的,JLabel 与字段的距离太远了:

Small

当我水平调整它时,结果是这样的:

Resized

这显然相差太远了。我用来设置这些组件的代码是:

public class LoginPanel extends JPanel {

private JTextField userfield = new JTextField(10);
private JPasswordField passfield = new JPasswordField(10);
private JButton login = new JButton("Login");
private JButton create = new JButton("Create Account");

public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.gridx = 2;
JLabel label = new JLabel("Username: ");
add(label, gbc);

gbc.gridx = 3;
gbc.gridwidth = 2;
add(userfield, gbc);

gbc.gridy = 1;
add(passfield, gbc);

gbc.gridx = 2;
label = new JLabel("Password: ");
add(label, gbc);

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 2;
gbc.gridx = 1;
gbc.gridwidth = 5;
add(new JSeparator(JSeparator.HORIZONTAL), gbc);
}
}

(删减了一些内容,如有遗漏请告知)我尝试过锚定,但我仍然不是 100% 熟悉 GridBagLayout(和约束),所以我不确定我的尝试是否在正确的方向。

如何防止 Username: Password: 标签从我的字段中移开,同时仍然能够调整大小?

此外,我想使用 GridBagLayout。我还有很多东西需要添加,我不想使用简单的布局,因为我需要灵 active 。

最佳答案

利用GridBagConstraints#anchor

login login

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class LogInTest {

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

public LogInTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new LoginPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class LoginPanel extends JPanel {

private JTextField userfield = new JTextField(10);
private JPasswordField passfield = new JPasswordField(10);
private JButton login = new JButton("Login");
private JButton create = new JButton("Create Account");

public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();

gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.gridx = 2;
gbc.anchor = GridBagConstraints.EAST;
JLabel label = new JLabel("Username: ");
add(label, gbc);

gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 3;
gbc.gridwidth = 2;
add(userfield, gbc);

gbc.gridy = 1;
add(passfield, gbc);

gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 2;
label = new JLabel("Password: ");
add(label, gbc);

gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 2;
gbc.gridx = 1;
gbc.gridwidth = 5;
add(new JSeparator(JSeparator.HORIZONTAL), gbc);
}
}

}

您可能还想考虑使用复合布局,即将每个区域分隔到它自己的容器中,并专注于每个部分的单独布局需求,然后将它们全部构建到一个布局中

关于java - 使用 GridBagLayout 调整大小时防止组件散开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23751314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com