1

我有一个 JPanel 和一个 Jlist,当用户在列表中选择不同的项目时,相应的组件将被添加到面板中,而之前的组件将被删除。这是代码的一部分:

depictorPanel.removeAll();
depictorPanel.invalidate();
depictorPanel.repaint();
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
depictorPanel.add(viewer, constraints);
viewer.setSize(depictorPanel.getSize());
depictorPanel.invalidate();
depictorPanel.repaint();

其中describeorPanel 是JPanel,查看器是组件(顺便说一句。查看器是来自JUNG 库的VisualizationView 类型,它也继承了JPanel)。

当我调整 JPanel 的大小(通过调整整个窗口的大小来完成,以便调整窗口中的所有组件的大小),或最小化窗口并恢复它时,查看器组件消失了,因为我设置了不同的背景颜色描述器面板和查看器。

我还处理了componentResizeddescribeorPanel 的侦听器以使查看器无效并重新绘制,但没有运气。

4

2 回答 2

1

我终于找到了一种使用布局管理器(GridBagLayout)的工作方法:

GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.BOTH;
constraints.gridx = 0;
constraints.gridy = 0;
constraints.weightx = 1;
constraints.weighty = 1;
depictorPanel.add(currentViewer, constraints);
depictorPanel.revalidate();

这段代码产生了我所期待的效果。我认为我的问题是没有很好地理解每个单独的布局管理器(因为我对 Java 比较陌生)。

谢谢大家!我确实通过这个问题(和 SSCCE)了解了一些关于 Java 的知识:-)

于 2013-09-28T17:27:30.417 回答
0

我找到了解决我的问题的方法,即在不使用任何布局管理器的情况下手动管理布局(手动设置位置和大小,并在调整 describeorPanel 大小时调整大小)。

于 2013-09-27T12:00:51.583 回答