java根面板中不显示所有单独的面板
我想向jpanel添加多个jpanel。所以我在jscrollpane中添加了一个根面板。然后将所有单独的JPanel添加到此根面板。我根据需要制定了jscrollpane的滚动策略。i、 水平滚动条根据需要,垂直滚动条根据需要。 但问题是所有单独的面板都没有显示在根面板中
代码:
JScrollPane scPanel=new JScrollPane();
JPanel rootPanel=new JPanel();
rootPanel.setLayout(new FlowLayout());
JPanel indPanel = new JPanel();
rootPanel.add(indPanel);
JPanel indPanel2 = new JPanel();
rootPanel.add(indPanel2);
//.....like this added indPanals to rootPanel.
scPanel.setViewPortView(rootPanel);
//scPanel.setHorizontalScrollPolicy(HORIZONTAL_SCROLLBAR_AS_NEEDED);
还有一件事是,当我滚动滚动滚动条时,面板会从jscrollpane区域消失。 我无法看到所有单独的面板, 请推荐我
编辑:来自double post的代码段:
MosaicFilesStatusBean mosaicFilesStatusBean = new MosaicFilesStatusBean();
DefaultTableModel tableModel = null;
tableModel = mosaicFilesStatusBean.getFilesStatusBetweenDates(startDate, endDate);
if (tableModel != null) {
rootPanel.removeAll();
rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.PAGE_AXIS));
for (int tempRow = 0; tempRow < tableModel.getRowCount(); tempRow++) {
int fileIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 0).toString());
String dateFromTemp = tableModel.getValueAt(tempRow, 3).toString();
String dateToTemp = tableModel.getValueAt(tempRow, 4).toString();
int processIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 5).toString());
int statusIdTemp = Integer.parseInt(tableModel.getValueAt(tempRow, 6).toString());
String operatingDateTemp = tableModel.getValueAt(tempRow, 7).toString();
MosaicPanel tempPanel =
new MosaicPanel(fileIdTemp, dateFromTemp, dateToTemp, processIdTemp, statusIdTemp, operatingDateTemp);
rootPanel.add(tempPanel);
}
rootPanel.revalidate();
}
# 1 楼答案
你看不到你的
JPanel
的主要原因是你使用FlowLayout
作为rootPanel
的LayoutManager
。因为你的JPanel
加在这个rootPanel
里面什么都没有,所以它的大小分别是0, 0
,宽度和高度。虽然使用GridLayout
这种情况不应该出现。请看附件中的代码示例:# 2 楼答案
不要对根面板使用FlowLayout。取而代之的是考虑使用{a1}:
编辑1
这里有一个SSCCE大致基于您发布的最新代码:
这个SSCCE很有效,因为它很容易显示如何将JPanel删除并添加到另一个由JScrollPane持有的JPanel中。如果您仍然有问题,您应该修改此SSCCE,使其显示您的问题