即使删除后对象也会重新绘制
我需要清除我的 JPanel
。这个名为 mainPane
的 JPanel
有一个 GridLayout
,其中包含 JScrollPane,而这些
JScrollPane包含自定义
code>JPanel重写
paintComponent()`。
无论我尝试什么(将列表设置为空,用新对象替换旧实例),只要我调整窗口框架的大小,就会调用视图的 paintComponent()
get 并在其中绘制视图再次进入mainPane
。
public void createContentPane()
{
gridLay = new GridLayout(GRID_ROWS, GRID_COLUMNS);
graphicViewList = new ArrayList<View>();
for (int i = 0; i < graphicViewList.size(); i++) {
View v = graphicViewList.get(i);
v = null;
}
mainPane = new JPanel();
clearGrid();
mainPane.removeAll();
mainPane.validate();
graphicViewList.clear();
imageModel.deleteObservers();
mainPane.setLayout(gridLay);
this.getContentPane().remove(mainPane);
this.getContentPane().add(mainPane, BorderLayout.CENTER);
mainPane.revalidate();
mainPane.repaint();
}
public void createViews()
{
int idx = 0;
graphicViewList.clear();
while(idx < NUM_PERSPECTIVE) //iterator?
{
if(idx == 0)
{
graphicViewList.add(new ThumbnailView(this, imageModel));
mainPane.add(new JScrollPane(graphicViewList.get(idx)));
idx++;
continue;
}
graphicViewList.add(new ImageView(this, imageModel));
mainPane.add(new JScrollPane(graphicViewList.get(idx)));
idx++;
}
for (int i = 0; i < graphicViewList.size(); i++)
graphicViewList.get(i).removeFocus();
this.getContentPane().add(mainPane, BorderLayout.CENTER);
}
private void clearGrid()
{
for (int i = 0; i < mainPane.getComponentCount(); i++) {
JScrollPane sP =(JScrollPane) mainPane.getComponent(i);
sP = null;
}
}
我知道 createContentPane()
太过分了,我只是绝望了。所以基本上第一个函数和第二个函数是在 GUI 构造函数中调用的。当我调用 createContentPane()
来替换旧的 UI(具有相同的结构,只是不同的内容)时,一旦我调整容器的大小,它的内容就会重新绘制。唯一的区别是,在第二个 createContentPane()
调用时,调整大小绘制会在布局上绘制,并且元素不再位于内部。
我认为 createContentPane() 会清空所有内容并删除我可能拥有的所有引用,但 PaintComponent() 仍然设法用视图绘制滚动条。
I need to clear my JPanel
. This JPanel
called mainPane
has a GridLayout
which contains JScrollPane's, and these
JScrollPanes contain custom
JPanels that override
paintComponent()`.
No matter what I try—setting the list to null, replacing the old instance with a new object—as soon as I resize the window frame, the view's paintComponent()
get's called and it draws the views inside the mainPane
again.
public void createContentPane()
{
gridLay = new GridLayout(GRID_ROWS, GRID_COLUMNS);
graphicViewList = new ArrayList<View>();
for (int i = 0; i < graphicViewList.size(); i++) {
View v = graphicViewList.get(i);
v = null;
}
mainPane = new JPanel();
clearGrid();
mainPane.removeAll();
mainPane.validate();
graphicViewList.clear();
imageModel.deleteObservers();
mainPane.setLayout(gridLay);
this.getContentPane().remove(mainPane);
this.getContentPane().add(mainPane, BorderLayout.CENTER);
mainPane.revalidate();
mainPane.repaint();
}
public void createViews()
{
int idx = 0;
graphicViewList.clear();
while(idx < NUM_PERSPECTIVE) //iterator?
{
if(idx == 0)
{
graphicViewList.add(new ThumbnailView(this, imageModel));
mainPane.add(new JScrollPane(graphicViewList.get(idx)));
idx++;
continue;
}
graphicViewList.add(new ImageView(this, imageModel));
mainPane.add(new JScrollPane(graphicViewList.get(idx)));
idx++;
}
for (int i = 0; i < graphicViewList.size(); i++)
graphicViewList.get(i).removeFocus();
this.getContentPane().add(mainPane, BorderLayout.CENTER);
}
private void clearGrid()
{
for (int i = 0; i < mainPane.getComponentCount(); i++) {
JScrollPane sP =(JScrollPane) mainPane.getComponent(i);
sP = null;
}
}
createContentPane()
is overkill I know, I'm just desperate. So basically the first function and the second function get called in the GUI constructor. When I make a call to createContentPane()
to replace the old UI (With the same structure, just different content), as soon as I resize the container, it's content are drawn anew. The only difference is that on the second createContentPane()
call the resize draw draws over the layout and the elements aren't inside anymore.
I would think that createContentPane()
would empty everything and removes all the references I might have, but paintComponent()
still manage's to draw the scrollbars with the views.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在clearGrid()中,您需要调用
remove(Component comp)
(在您的情况下为sP)或removeAll();
In clearGrid(), you need to call
remove(Component comp)
(in your case, sP) orremoveAll();
在您的clearGrid方法中,您没有从面板中删除任何内容;您只是获得对 mainPane 内部内容的引用,然后将该引用设置为 null。相反,您需要为每个滚动窗格实际调用
mainPane.remove(sP);
。另外,您可以尝试调用
mainPane.removeAll();
,而不是该循环。请注意,如文档中所述,您必须随后重新验证面板。Container.removeAll
这是 Andrew Thompson 之前提到的 SSCCE。以后在提出问题时尝试这样做,这样人们就可以运行它并帮助修复它。
In your clearGrid method, you are not removing anything from the panel; you are just getting a reference to what's inside the mainPane and then setting that reference to null. Instead, you need to actually call
mainPane.remove(sP);
for each scroll pane.Also, instead of that loop, you could try calling
mainPane.removeAll();
. Note, as mentioned in the documentation, you have to revalidate the panel afterwards.Container.removeAll
Here's that SSCCE that Andrew Thompson mentioned previously. Try doing this in the future when asking questions, so people can just run it and help fix it.