添加另一个JPanel后,JPanel搞砸了

时间:2023-01-28 16:02:17

Hi I'm making my ui with JFrame and this is my code

嗨,我正在用JFrame制作我的ui,这是我的代码

private static void prepareUI(JPanel jPanel) {
    jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
    jPanel.add(new JLabel("Enter Full Text Below"));

    {
        textInput = new JTextArea(5, 1);
        JScrollPane jScrollPane = new JScrollPane(textInput);
        jPanel.add(jScrollPane);
    }

    {
        JSeparator jSeparator = new JSeparator(SwingConstants.HORIZONTAL);
        jSeparator.setMaximumSize(new Dimension(Integer.MAX_VALUE, 1));
        jPanel.add(jSeparator);
    }

    jPanel.add(new JLabel("Or Enter URL And Press Enter"));

    //url
    {
        urlInput = new JTextField(1);
        jPanel.add(urlInput);
    }

    {
        JSeparator jSeparator = new JSeparator(SwingConstants.HORIZONTAL);
        jSeparator.setMaximumSize(new Dimension(Integer.MAX_VALUE, 1));
        jPanel.add(jSeparator);
    }

    jPanel.add(new JLabel("Or"));

    {
        JPanel jPanel1 = new JPanel();
        jPanel1.setLayout(new BoxLayout(jPanel1,BoxLayout.X_AXIS));
        JButton jButton = new JButton("Browse");
        JTextField jTextField = new JTextField(1);
        jPanel1.add(jTextField);
        jPanel1.add(jButton);
        jPanel.add(jPanel1);
    }

    {
        JButton jButton = new JButton("Find Matches");
        jPanel.add(jButton, BorderLayout.CENTER);
    }

    {
        JButton jButton = new JButton("Matches");
        jPanel.add(jButton, BorderLayout.CENTER);
    }
}

but the output is not what i expected

但输出并不是我的预期

添加另一个JPanel后,JPanel搞砸了

you can see the second JTextField new Browse JButton is so small and not taking the whole parent and also JLabels are not in the left side. How can i fix it?

你可以看到第二个JTextField新浏览JButton是如此之小,并没有占据整个父级,而且JLabel也不在左侧。我该如何解决?

Edit: I removed setMaximumSize(new Dimension(Integer.MAX_VALUE, 1)) and added column size 1 to all JTextFields but it's not fixed yet. JTextField is more than 1 line height and JLabel is not in left side.

编辑:我删除了setMaximumSize(new Dimension(Integer.MAX_VALUE,1))并将列大小1添加到所有JTextFields,但它尚未修复。 JTextField超过1行高,JLabel不在左侧。

2 个解决方案

#1


1  

jTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 1));

Don't try to manually set a size restriction. I would guess a components can't display with a height of 1.

不要尝试手动设置大小限制。我猜想组件不能以1的高度显示。

To suggest a size for a JTextField you would use:

要建议JTextField的大小,您可以使用:

JTextField textField = new JTextField(10);

Then the text field will be sized to hold 10 "W" characters.

然后文本字段将调整大小以容纳10“W”字符。

Edit:

Also, when you create a JTextArea you use:

此外,当您创建JTextArea时,您使用:

JTextArea textArea = new JTextArea(5, 20);

to suggest the number of row/columns you need. The component will then calculate its size.

建议您需要的行数/列数。然后组件将计算其大小。

#2


0  

It is better to use different Layout for JPanel. For your design, you can use GridBagLayout. It is responsive and makes management of your widgets easier.

最好为JPanel使用不同的布局。对于您的设计,您可以使用GridBagLayout。它具有响应性,可以更轻松地管理您的小部件。

#1


1  

jTextField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 1));

Don't try to manually set a size restriction. I would guess a components can't display with a height of 1.

不要尝试手动设置大小限制。我猜想组件不能以1的高度显示。

To suggest a size for a JTextField you would use:

要建议JTextField的大小,您可以使用:

JTextField textField = new JTextField(10);

Then the text field will be sized to hold 10 "W" characters.

然后文本字段将调整大小以容纳10“W”字符。

Edit:

Also, when you create a JTextArea you use:

此外,当您创建JTextArea时,您使用:

JTextArea textArea = new JTextArea(5, 20);

to suggest the number of row/columns you need. The component will then calculate its size.

建议您需要的行数/列数。然后组件将计算其大小。

#2


0  

It is better to use different Layout for JPanel. For your design, you can use GridBagLayout. It is responsive and makes management of your widgets easier.

最好为JPanel使用不同的布局。对于您的设计,您可以使用GridBagLayout。它具有响应性,可以更轻松地管理您的小部件。