您好我是Java新手。我有JPanel,其他添加了JPanel。内部JPanel的数量取决于列表。如果列表是删除或添加,那么我删除所有内部JPanel并根据更新列表添加JPanel。内部面板将具有取决于列表数据的不同高度。我的问题是内部面板的高度将改变以适应外部JPanel,因为内部面板的总量变化。我希望当我删除或添加新的JPanel内部时,内部面板的高度不会受到影响。我使用BoxLayout作为外部面板。有人会告诉我如何解决它。提前谢谢。
我将JPanel内部的代码添加到局外人JPanel
中 jpListCommentPane=new JPanel();
jpListCommentPane.setLayout(new BoxLayout(jpListCommentPane,
BoxLayout.Y_AXIS));
//sortPage
Component[] components = jpListCommentPane.getComponents();
for (Component component : components) {
jpListCommentPane.remove(component);
jpListCommentPane.repaint();
}
sortComment= lstComment;
Collections.sort(sortComment,CommentItem.sortPage);
for(CommentItem comm: sortComment){
DivCommentJPane d=new DivCommentJPane(comm, this);
jpListCommentPane.add(d);
}
jpListCommentPane.revalidate();
有屏幕截图用于比较: