gpt4 book ai didi

java - 排列多个不同高度的 JPanel

转载 作者:行者123 更新时间:2023-11-30 09:27:50 24 4
gpt4 key购买 nike

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

面板包含一个带有折叠/展开按钮的标题子面板和一个带有 JTable 和一些 JButton 的内容子面板。

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

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

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

如果我将 TEST_NUMBER_OF_PANELS 设置为 50,它的行为就像 excpeted。如果我将 TEST_NUMBER_OF_PANELS 设置为 2,面板的高度将调整为填充整个滚动 Pane 。

我使用 swingx 库中的 JXCollapsiblePane 作为内容子面板。如果我折叠一个面板并且只有几个面板可见,LayoutManager 开始调整标题子面板的大小,它应该始终保持相同的大小。(我想那是我不正确使用 LayoutManager 的后续错误)

以下 SSCCE 无需任何外部库即可编译。我在用 JPanel 替换 JXCollapsiblePane 的地方添加了“TODO: un/comment line”。(导入、初始化、绑定(bind))

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

提前谢谢你

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));
}
}
}

最佳答案

您可以将您的面板封装到另一个布局中以尽可能减小尺寸(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) 的选择使得外部容器缩小包含的面板。

关于java - 排列多个不同高度的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14322554/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com