安排多个不同高度的JPanel

时间:2013-01-14 16:33:28

标签: java swing layout-manager

我试图在JScrollPane中显示多个JPanel。

面板包含一个带有折叠/展开按钮的header-subpanel和一个带有JTable和一些JButton的content-subpanel。

每个Panel的高度由Panel内JTable的大小决定。

如果没有足够的JPanel来填充JScrollPane,剩下的空间应该是空的。

我的BoxLayout方法效果很好,只要有足够的Panel。

如果我将 TEST_NUMBER_OF_PANELS 设置为50,则表现得像是被激活了。 如果我将 TEST_NUMBER_OF_PANELS 设置为2,则会调整Panel的高度以填充整个滚动窗格。

我使用swingx库中的JXCollapsiblePane作为content-subpanel。 如果我折叠一个Panel并且只有几个面板可见,则LayoutManager开始调整header-subpanel的大小,该子面板应始终保持相同的大小。 (我猜这是我错误使用LayoutManager的后续错误)

以下SSCCE编译时没有任何外部库。 我添加了“TODO:un / comment line”,其中我用JPanel替换了JXCollapsiblePane。 (导入,初始化,绑定)

我不确定哪个LayoutManager最适合安排我的面板。 我想我可以放弃一些JPanels,如果我正确使用LayoutManager。

提前谢谢

package test;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Random;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

// TODO: uncomment line
//import org.jdesktop.swingx.JXCollapsiblePane;

public class Test extends JFrame {
    private static final int TEST_NUMBER_OF_PANELS = 50;
    private static JTabbedPane tabbedPane = new JTabbedPane();
    Test() {
        this.setLayout(new BorderLayout());
        this.setSize(1050, 700);
        this.setMinimumSize(new Dimension(400,200));
        this.add(tabbedPane, BorderLayout.CENTER);

        tabbedPane.addTab("tab1", new TestTabContent());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new Test().setVisible(true);
            }
        });
    }

    private class TestTabContent extends JPanel {
        TestTabContent() {
            JPanel boxContainer = new JPanel();
            boxContainer.setLayout(new BoxLayout(boxContainer, BoxLayout.Y_AXIS));
            JScrollPane mainScrollPane = new JScrollPane(boxContainer);

            // create toolbar
            JPanel toolBar = new JPanel();
            toolBar.setLayout(new BorderLayout());

            //east
            JPanel InfoPanel = new JPanel();
            InfoPanel.setLayout(new BoxLayout(InfoPanel, BoxLayout.X_AXIS));
            InfoPanel.add(new JLabel("test: some info ..."));
            toolBar.add(InfoPanel, BorderLayout.WEST);
            //west
            JPanel viewOptionPanel = new JPanel();
            viewOptionPanel.setLayout(new BoxLayout(viewOptionPanel, BoxLayout.X_AXIS));
            viewOptionPanel.add(new JLabel("some controls.."));
            toolBar.add(viewOptionPanel, BorderLayout.EAST);

            // set main panel´s layout
            GroupLayout layout = new GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(toolBar, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(mainScrollPane)
                    );
            layout.setVerticalGroup(
                    layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                            .addComponent(toolBar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, 0)
                            .addComponent(mainScrollPane, GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE))
                    );

            // create controls 4 test ...
            for (int i = 0; i < TEST_NUMBER_OF_PANELS; i++) {
                TestPanel newTestPanel = new TestPanel();

                // seperator panels for spacing
                JPanel seperator = new JPanel(new BorderLayout());
                seperator.setBackground(Color.black);
                seperator.add(newTestPanel);

                boxContainer.add(seperator);
            }
        }
    }
    private class TestPanel extends JPanel { 
        //private static final Icon COLLAPSE_ICON = new ImageIcon(Test.class.getResource("images/collapse_1616.png"));
        //private static final Icon EXPAND_ICON = new ImageIcon(Test.class.getResource("images/expand_1616.png"));
        private JTable table;
        private DefaultTableModel tableModel;
        private JButton collapsingButton;

        // TODO: uncomment line
        //private JXCollapsiblePane collapsiblePane = new JXCollapsiblePane();
        // TODO: comment line
        private JPanel collapsiblePane = new JPanel();

        public TestPanel() {

            this.setLayout(new BorderLayout()); 

            this.setBorder(BorderFactory.createLineBorder(Color.BLACK));

            // container with boxLayout for collapsiblePane
            JPanel boxContainer = new JPanel();
            boxContainer.setLayout(new BoxLayout(boxContainer, BoxLayout.Y_AXIS));
            boxContainer.setBorder(BorderFactory.createMatteBorder(0, 1, 1, 1, Color.BLACK));

            // set table stuff
            tableModel = new DefaultTableModel();
            // column headers
            Vector<String> title = new Vector<String>();
            title.add("A");
            title.add("B");
            title.add("C");
            title.add("D");
            // some random data
            Random randomGenerator = new Random();
            int rnd = randomGenerator.nextInt(10) + 1;
            Vector<Vector<String>> data = new Vector<Vector<String>>();
            Vector<String> row = new Vector<String>();
            for (int i=0; i<rnd; i++) {
                row.add("1");
                row.add("2");
                row.add("3");
                row.add("4");
                data.add(row);
            }
            tableModel.setDataVector(data, title);
            table = new JTable(tableModel);

            boxContainer.add(table.getTableHeader(), BorderLayout.NORTH);
            boxContainer.add(table, BorderLayout.CENTER);

            // other controls / toolbar
            JPanel toolbar = new JPanel();
            toolbar.setLayout(new BorderLayout());

            // buttons to the right
            JPanel toolbarButtonGroup = new JPanel();
            toolbarButtonGroup.setLayout(new BoxLayout(toolbarButtonGroup, BoxLayout.X_AXIS));

            // Button1
            JButton button = new JButton("Button1");
            JPanel sepPanel = new JPanel();
            sepPanel.add(button);
            toolbarButtonGroup.add(sepPanel);

            // Button2
            button = new JButton("Button2");
            sepPanel = new JPanel();
            sepPanel.add(button);
            toolbarButtonGroup.add(sepPanel);

            // Button3
            button = new JButton("Button3");
            sepPanel = new JPanel();
            sepPanel.add(button);

            toolbarButtonGroup.add(sepPanel);

            toolbar.add(toolbarButtonGroup, BorderLayout.EAST);
            boxContainer.add(toolbar);

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

            // add panel with collapse/expand button
            JPanel buttonPanel = new JPanel(); 
            buttonPanel.setLayout(new BorderLayout());
            buttonPanel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 24));

            collapsingButton = new JButton("foo"); // , COLLAPSE_ICON
            collapsingButton.setName("toggleButton");
            collapsingButton.setHorizontalAlignment(SwingConstants.LEFT);
            collapsingButton.setBorderPainted(false);
            collapsingButton.setFocusPainted(false);

            buttonPanel.add(collapsingButton, BorderLayout.CENTER); 
            buttonPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

            subPanel.add(buttonPanel);

            collapsiblePane.setName("collapsiblePane"); 
            collapsiblePane.setLayout(new CardLayout()); 

            collapsiblePane.add(boxContainer, ""); 
            subPanel.add(collapsiblePane); 

            add(subPanel);

            // TODO: uncomment line
            //collapsingButton.addActionListener(collapsiblePane.getActionMap().get( 
            //      JXCollapsiblePane.TOGGLE_ACTION)); 
        } 
    } 
}

1 个答案:

答案 0 :(得分:2)

您可以将面板封​​装到另一个布局中,尽可能减小尺寸(TestTabContent()内的代码):

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

JPanel boxContainerOuter = new JPanel();
boxContainerOuter.setLayout(new BorderLayout());
boxContainerOuter.add(boxContainer, BorderLayout.NORTH);

JScrollPane mainScrollPane = new JScrollPane(boxContainerOuter);

在上面的代码中,boxContainer包含在另一个小组boxContainerOuter中。选择其布局和位置(BoxLayout.NORTH),以便外部容器缩小包含的面板。