调整JPanel的高度

时间:2016-07-20 20:28:00

标签: java swing layout-manager flowlayout

enter image description here我有一个顶部面板,其流程布局包含文本字段和标签。我需要在第一行下面添加更多文本字段和标签。我意识到在flowlayout中没有真正强制换行的方法所以我只是想制作一个新的面板并将其置于顶部面板之下。现在的问题是因为顶板的高度太高而存在差距。有没有办法调整面板大小,使其宽度自动调整到屏幕,但我想调整新面板的高度,因此间隙较小。顶部的两个文本字段位于顶部面板中,带有一个flowlayout,下面的3个文本字段位于一个带有flowlayout的新面板中,有没有办法让3个文本字段在2个文本字段的正下方?此外,整个框架都在网格布局中。

frame.setLayout(new GridLayout(0,1));
Static JPanel topPanel = new JPanel(new flowlayout);
Static JPanel searchPanel = new JPanel(new flowlayout);
Static JPanel statusPanel = new JPanel(new gridLayout(2,1));
Static JPanel categoryPanel = new JPanel(new GridLayout(0.2));
Static JPanel btnPanel = new JPanel(new GridLayout(0,3));
frame.pack();
topPanel.add(searchPanel);
JPanel container2 = new JPanel(new BorderLayout()):
container2.add(topPanel, BorderLayout.North);
frame.getContentPane().add(container2);
frame.getContentPane().add(statusPanel);
frame.getContentPane().add(catergoryPanel);
frame.getContentPane().add(btnPanel);

1 个答案:

答案 0 :(得分:4)

JPanel GridLayout是问题所在 - 它可能已添加到容器的BorderLayout.CENTER,因此它会延伸到整个可用空间。它也具有相同的单元尺寸。将网格包装到BorderLayout并将其添加到BorderLayout.NORTH或使用GridBagLayout作为面板。

enter image description here

这是一个示例,演示了如何在您的示例中布局面板以及如何在其他布局中布局。我认为这会清楚说明为什么GridLayout以这种方式行事。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Example {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Example();
            }
        });
    }

    public Example() {

        // GridLayout

        JPanel container1 = new JPanel(new GridLayout(2, 1));
        container1.add(createExamplePanel());
        container1.add(createExamplePanel());

        // GridLayout inside BorderLayut (NORTH)

        JPanel panel = new JPanel(new GridLayout(2, 1, 0, 5 /* margin */)); // You could also use other layouts here, e.g. a BoxLayout
        panel.add(createExamplePanel());
        panel.add(createExamplePanel());
        JPanel container2 = new JPanel(new BorderLayout());
        container2.add(panel, BorderLayout.NORTH);

        // GridBagLayout

        JPanel container3 = new JPanel(new GridBagLayout());
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor = GridBagConstraints.PAGE_START;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(0, 0, 5 /* margin */, 0);
        constraints.weightx = 1.0;
        container3.add(createExamplePanel(), constraints);
        constraints.gridy = 1;
        constraints.weighty = 1.0;
        constraints.insets = new Insets(0, 0, 0, 0);
        container3.add(createExamplePanel(), constraints);

        JPanel contentPane = new JPanel(new GridLayout(1, 3, 5, 5));
        contentPane.add(createTitledPanel(container1, "GridLayout"));
        contentPane.add(createTitledPanel(container2, "GridLayout inside BorderLayout (NORTH)"));
        contentPane.add(createTitledPanel(container3, "GridBagLayout"));

        JFrame frame = new JFrame("Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(contentPane);
        frame.setSize(800, 600);
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    private JPanel createTitledPanel(JComponent content, String title) {
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(content);
        panel.setBorder(BorderFactory.createTitledBorder(title));
        return panel;
    }

    private JPanel createExamplePanel() {
        JPanel panel = new JPanel();
        panel.add(new JLabel("Panel"));
        panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        return panel;
    }

}