gpt4 book ai didi

java - Spring布局中组件之间的空间太大

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:29:46 24 4
gpt4 key购买 nike

我想手动创建一个 JFrame 并使用 spring 布局来执行此操作。但是,我的最终输出并不好。我的行与单选按钮之间的空间太大了:

example output

我的代码:

public final class NewUserFrame1 extends JFrame {

public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
pack();
}

public JPanel rowComponent() {

JPanel panel = new JPanel();
JLabel fnamelbl = new JLabel("First name");
JLabel lnamelbl = new JLabel("Last Name");
JLabel fntemp = new JLabel();
JLabel lntemp = new JLabel();
JTextField fntf = new JTextField(10);
JTextField lntf = new JTextField(10);
JLabel gndlnl = new JLabel("Gender");
JRadioButton malerb = new JRadioButton("Male");
JRadioButton femalerb = new JRadioButton("Female");
ButtonGroup bgroup = new ButtonGroup();
bgroup.add(malerb);
bgroup.add(femalerb);
JLabel registnm = new JLabel("Registration ID is:");
JLabel showreglbl = new JLabel();
JLabel regtemp = new JLabel();

panel.add(fnamelbl);
panel.add(fntf);
panel.add(fntemp);
panel.add(lnamelbl);
panel.add(lntf);
panel.add(lntemp);
panel.add(gndlnl);
panel.add(malerb);
panel.add(femalerb);
panel.add(registnm);
panel.add(showreglbl);
panel.add(regtemp);

panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);
return panel;
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
NewUserFrame1 newUserFrame1 = new NewUserFrame1();
}
});
}
}

现在: enter image description here

最佳答案

不是在 NewUserFrame1 构造函数中调用 setSize,而是调用 JFrame 上的 pack

public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
//setSize(800, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
pack();
}

同时更改 SpringUtilities.makeCompactGrid 方法的参数如下:

SpringUtilities.makeCompactGrid(panel, 4, 3, 50, 15, 3, 4);//change yPad to 4 instead of 100. It sets the vertical height between two rows

关于java - Spring布局中组件之间的空间太大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15896426/

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