ComponentListener 出现问题。我用它来检查某个组件的大小是否已调整,如果是,则更新一些内容。
代码看起来像这样,尽管这可能对您没有多大用处:
@Override
public void componentResized(ComponentEvent e) {
// Graph resized, reposition slice nodes
Component c = e.getComponent();
if (graphHeight > 0) {
if (c == graph) {
int offset = (c.getHeight() - graphHeight) / 2;
if (offset != 0) {
try {
controller.shiftSlice1NodesY(offset);
} catch (GraphIntegrityException e1) {
logger.error(e1.getMessage(), e1);
}
graphHeight = c.getHeight();
}
}
} else {
graphHeight = c.getHeight();
}
}
对于我的代码的一部分,我需要禁用此监听器。看起来像这样:
graph.removeComponentListener(this);
controller.parseFile(filename);
graph.addComponentListener(this);
一切都很顺利,直到我再次添加组件监听器。此时componentResized方法被调用了大约20次。从我所看到的来看,它可能缓冲了调整大小事件。有人知道这是怎么回事吗?
最佳答案
您似乎正在尝试修改事件调度线程 (EDT) 之外的组件。这在 AWT 中甚至不是一个好主意,更不用说 Swing 了。
除了在正确的线程上获取所有 AWT/Swing 内容之外,修复的一部分应该是获取监听器检查状态以查看它是否应该执行其主体,而不是尝试删除和添加监听器。
关于Java - Swing/AWT ComponentListener 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2541809/