根据jpanel中存在的组件调整jpanel的大小

时间:2014-03-30 14:40:21

标签: java swing layout jpanel

在下面的示例中,在边框布局的西侧,有一个父面板,其中包含BoxLayout和几个面板。问题是西面板从上到下覆盖整个区域。父面板中用于子面板的FlowLayout占用了大量区域。是否可以根据组件压缩每个JPanel?此外,即使窗口最大化,它应该保持不变?

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

public class Sample extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Sample frame = new Sample();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Sample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("New label");
        panel.add(lblNewLabel);

        JPanel panel_1 = new JPanel();
        contentPane.add(panel_1, BorderLayout.WEST);
        panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);

        textField = new JTextField();
        panel_2.add(textField);
        textField.setColumns(2);

        textField_1 = new JTextField();
        panel_2.add(textField_1);
        textField_1.setColumns(2);

        JPanel panel_3 = new JPanel();
        panel_1.add(panel_3);

        JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
        panel_3.add(chckbxNewCheckBox);

        JPanel panel_4 = new JPanel();
        panel_1.add(panel_4);

        JButton btnNewButton = new JButton("New");
        panel_4.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New");
        panel_4.add(btnNewButton_1);
    }

}

1 个答案:

答案 0 :(得分:3)

一种方法是将panel_1添加到封闭面板。当FlowLayout封闭pack()时,默认Window符合所附组件的首选大小。我在CENTER添加了一个灰色面板作为占位符;调整框架大小以查看效果。

JPanel flowPanel = new JPanel();
flowPanel.add(panel_1);

image

经测试:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;

public class Sample extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    Sample frame = new Sample();
                    frame.pack();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Sample() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("New label");
        panel.add(lblNewLabel);

        JPanel panel_1 = new JPanel();
        panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.Y_AXIS));

        JPanel panel_2 = new JPanel();
        panel_1.add(panel_2);

        textField = new JTextField();
        panel_2.add(textField);
        textField.setColumns(2);

        textField_1 = new JTextField();
        panel_2.add(textField_1);
        textField_1.setColumns(2);

        JPanel panel_3 = new JPanel();
        panel_1.add(panel_3);

        JCheckBox chckbxNewCheckBox = new JCheckBox("New check box");
        panel_3.add(chckbxNewCheckBox);

        JPanel panel_4 = new JPanel();
        panel_1.add(panel_4);

        JButton btnNewButton = new JButton("New");
        panel_4.add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New");
        panel_4.add(btnNewButton_1);

        JPanel flowPanel = new JPanel();
        flowPanel.add(panel_1);
        contentPane.add(flowPanel, BorderLayout.WEST);
        contentPane.add(new JPanel(){

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(320, 240);
            }

            @Override
            public Color getBackground() {
                return Color.lightGray;
            }

        }, BorderLayout.CENTER);
    }

}