如何在Java GUI Swing中设置布局?

huangapple go评论73阅读模式
英文:

How to set layout in java GUI swing?

问题

以下是翻译好的内容:

private JPanel SetUpRegister() {
    JLabel registerLabel = new JLabel("注册");

    registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
    loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    String[] genderlist = new String[] { "男性", "女性", "其他" };
    JComboBox<String> registerList = new JComboBox<>(genderlist);

    JPanel registerPanel = new JPanel();
    registerPanel.setBackground(new Color(255, 140, 0));
    registerPanel.add(registerLabel);
    registerPanel.add(loginRegisterInput);
    registerPanel.add(passwordRegisterInput);
    registerPanel.add(fnRegisterInput);
    registerPanel.add(lnRegisterInput);
    registerPanel.add(ageRegisterInput);
    registerPanel.add(registerList);
    registerPanel.setLayout(new BoxLayout(registerPanel, BoxLayout.Y_AXIS));

    return registerPanel;
}
英文:

I am trying to create a register form for my application(school project), I wanted to set the layout to BoxLayout but the Jtextfields and combo box is having issue as you can see below, does this issue relates to setSize() or is it something I am doing incorrect,I just want the Jtextfields sorts vertically , I appreciate the support
如何在Java GUI Swing中设置布局?

private JPanel SetUpRegister() {
		JLabel registerLabel = new JLabel(&quot;Registera&quot;);
		
		registerLabel.setFont(new Font(&quot;Arial&quot;, Font.BOLD, 30));
		loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
		passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
		fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
		lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
		ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
		String[] genderlist = new String[] { &quot;Male&quot;, &quot;Female&quot;, &quot;Other&quot; };
		JComboBox&lt;String&gt; registerList = new JComboBox&lt;&gt;(genderlist);
		
		JPanel registerPanel = new JPanel();	
		registerPanel.setBackground(new Color(255, 140, 0));
		registerPanel.add(registerLabel);
		registerPanel.add(loginRegisterInput);
		registerPanel.add(passwordRegisterInput);
		registerPanel.add(fnRegisterInput);
		registerPanel.add(lnRegisterInput);
		registerPanel.add(ageRegisterInput);
		registerPanel.add(registerList);
		registerPanel.setLayout(new BoxLayout(registerPanel,BoxLayout.Y_AXIS));
		
		return registerPanel;

}

答案1

得分: 1

> 输入字段非常大

BoxLayout 将在面板上有多余空间时尝试调整组件的大小。它会将组件的大小调整到其最大尺寸。

由于某种原因,JTextField 的最大高度是 Integer.MAX_VALUE,这对我来说毫无意义,因为随着输入更多文本,文本的高度从不改变。

无论如何,您有几个选择:

  1. 使用不同的布局管理器,如 GridBagLayoutGridBagLayout 将尊重文本字段的首选大小。
  2. 创建一个自定义的 JTestField,并覆盖 getMaximumSize() 方法,使其返回组件的首选高度。
  3. 使用一个包装面板。

对于包装面板,您可以这样做:

JPanel wrapper = new JPanel(new BorderLayout());
wrapper.add(registerPanel, BorderLayout.PAGE_START);
return wrapper;
//return registerPanel;

BorderLayout 将尊重添加到 PAGE_START 的任何组件的首选高度,因此不需要 BoxLayout 调整任何组件的大小。

英文:

> The input fields are huge

The BoxLayout will attempt to resize components when extra space is available on the panel. It will resize a component up to its maximum size.

For some reason the maximum height of a JTextField is Integer.MAX_VALUE which makes no sense to me, since the height of the text never changes as you enter more text.

In any case you have a couple of choices:

  1. Use a different layout manager, like the GridBagLayout. The GridBagLayout, will respect the preferred size of the text fields.
  2. Create a custom JTestField and override the getMaximumSize() method to return the preferred height of the component
  3. Use a wrapper panel.

For the wrapper panel you could do:

JPanel wrapper = new JPanel( new BorderLayout() );
wrapper.add(registerPanel, BorderLayout.PAGE_START);
return wrapper;
//return registerPanel;

The BorderLayout will respect the preferred height of any component added to the PAGE_START, so there is no need for the BoxLayout to resize any component.

答案2

得分: 0

private JPanel SetUpRegister() {
    JLabel registerLabel = new JLabel("注册");
    JLabel registerLabel1 = new JLabel("登录:");
    JLabel registerLabel2 = new JLabel("密码:");
    JLabel registerLabel3 = new JLabel("名字:");
    JLabel registerLabel4 = new JLabel("姓氏:");
    JLabel registerLabel5 = new JLabel("年龄:");
    JLabel registerLabel6 = new JLabel("性别:");
    JLabel registerLabel7 = new JLabel("个人简介:");
    registerLabel.setFont(new Font("Arial", Font.BOLD, 30));
    JButton createAccButton = new JButton("创建");
    createAccButton.addActionListener(new CreateAccountListener());
    loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
    bioRegisterInput = new JTextField(INPUT_FIELD_WIDTH);

    String[] genderlist = new String[] { "男", "女", "其他" };
    registerList = new JComboBox(genderlist);
    JPanel registerPanel = new JPanel();
    registerPanel.setLayout(new BoxLayout(registerPanel, BoxLayout.Y_AXIS));
    registerPanel.add(registerLabel);

    JPanel registerLabPanel = new JPanel();
    registerLabPanel.setLayout(new FlowLayout());
    registerLabPanel.add(registerLabel);

    JPanel usernamePanel = new JPanel();
    usernamePanel.setLayout(new FlowLayout());
    usernamePanel.add(registerLabel1);
    usernamePanel.add(loginRegisterInput);

    JPanel passwordPanel = new JPanel();
    passwordPanel.setLayout(new FlowLayout());
    passwordPanel.add(registerLabel2);
    passwordPanel.add(passwordRegisterInput);

    JPanel fnPanel = new JPanel();
    fnPanel.setLayout(new FlowLayout());
    fnPanel.add(registerLabel3);
    fnPanel.add(fnRegisterInput);

    JPanel lnPanel = new JPanel();
    lnPanel.setLayout(new FlowLayout());
    lnPanel.add(registerLabel4);
    lnPanel.add(lnRegisterInput);

    JPanel agePanel = new JPanel();
    agePanel.setLayout(new FlowLayout());
    agePanel.add(registerLabel5);
    agePanel.add(ageRegisterInput);

    JPanel genderPanel = new JPanel();
    genderPanel.setLayout(new FlowLayout());
    genderPanel.add(registerLabel6);
    genderPanel.add(registerList);

    JPanel bioPanel = new JPanel();
    bioPanel.setLayout(new FlowLayout());
    bioPanel.add(registerLabel7);
    bioPanel.add(bioRegisterInput);

    JPanel buttonLoginPanel = new JPanel();
    buttonLoginPanel.setLayout(new FlowLayout());
    buttonLoginPanel.add(createAccButton);

    registerPanel.add(registerLabel);
    registerPanel.add(usernamePanel);
    registerPanel.add(passwordPanel);
    registerPanel.add(fnPanel);
    registerPanel.add(lnPanel);
    registerPanel.add(agePanel);
    registerPanel.add(genderPanel);
    registerPanel.add(bioPanel);
    registerPanel.add(buttonLoginPanel);

    return registerPanel;
}

I've translated the code as per your request. If you have any further questions or need additional assistance, feel free to ask.

英文:
private JPanel SetUpRegister() {
JLabel registerLabel = new JLabel(&quot;Registera&quot;);
JLabel registerLabel1 = new JLabel(&quot;Login :&quot;);
JLabel registerLabel2 = new JLabel(&quot;Password :&quot;);
JLabel registerLabel3 = new JLabel(&quot;First Name :&quot;);
JLabel registerLabel4 = new JLabel(&quot;Last Name :&quot;);
JLabel registerLabel5 = new JLabel(&quot;Age :&quot;);
JLabel registerLabel6 = new JLabel(&quot;Gender :&quot;);
JLabel registerLabel7 = new JLabel(&quot;Bio :&quot;);
registerLabel.setFont(new Font(&quot;Arial&quot;, Font.BOLD, 30));
JButton createAccButton = new JButton(&quot;Create&quot;);
createAccButton.addActionListener(new CreateAccountListener());
loginRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
passwordRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
fnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
lnRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
ageRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
bioRegisterInput = new JTextField(INPUT_FIELD_WIDTH);
String[] genderlist = new String[] { &quot;Male&quot;, &quot;Female&quot;, &quot;Other&quot; };
registerList = new JComboBox(genderlist);
JPanel registerPanel = new JPanel();
registerPanel.setLayout(new BoxLayout(registerPanel, BoxLayout.Y_AXIS));
registerPanel.add(registerLabel);
JPanel registerLabPanel = new JPanel();
registerLabPanel.setLayout(new FlowLayout());
registerLabPanel.add(registerLabel);
JPanel usernamePanel = new JPanel();
usernamePanel.setLayout(new FlowLayout());
usernamePanel.add(registerLabel1);
usernamePanel.add(loginRegisterInput);
JPanel passwordPanel = new JPanel();
passwordPanel.setLayout(new FlowLayout());
passwordPanel.add(registerLabel2);
passwordPanel.add(passwordRegisterInput);
JPanel fnPanel = new JPanel();
fnPanel.setLayout(new FlowLayout());
fnPanel.add(registerLabel3);
fnPanel.add(fnRegisterInput);
JPanel lnPanel = new JPanel();
lnPanel.setLayout(new FlowLayout());
lnPanel.add(registerLabel4);
lnPanel.add(lnRegisterInput);
JPanel agePanel = new JPanel();
agePanel.setLayout(new FlowLayout());
agePanel.add(registerLabel5);
agePanel.add(ageRegisterInput);
JPanel genderPanel = new JPanel();
genderPanel.setLayout(new FlowLayout());
genderPanel.add(registerLabel6);
genderPanel.add(registerList);
JPanel bioPanel = new JPanel();
bioPanel.setLayout(new FlowLayout());
bioPanel.add(registerLabel7);
bioPanel.add(bioRegisterInput);
JPanel buttonLoginPanel = new JPanel();
buttonLoginPanel.setLayout(new FlowLayout());
buttonLoginPanel.add(createAccButton);
registerPanel.add(registerLabel);
registerPanel.add(usernamePanel);
registerPanel.add(passwordPanel);
registerPanel.add(fnPanel);
registerPanel.add(lnPanel);
registerPanel.add(agePanel);
registerPanel.add(genderPanel);
registerPanel.add(bioPanel);
registerPanel.add(buttonLoginPanel);
return registerPanel;
}

I fixed the issue by making a Panel for each input and label

huangapple
  • 本文由 发表于 2020年5月4日 16:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61587773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定