Java - Swing / AWT ComponentListener 意外行为

发布于 2024-08-26 20:56:07 字数 1014 浏览 5 评论 0原文

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次。从我所看到的来看,它可能缓冲了调整大小事件。有人知道这是怎么回事吗?

Got a problem with ComponentListener. I'm using it to check if a certain component is resized, and to update some stuff if it is.

The code looks like this though this probably won't be much use to you:

@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();
    }
}

For one section of my code I need to disable this listener. That looks like this:

    graph.removeComponentListener(this);

    controller.parseFile(filename);

    graph.addComponentListener(this);

Everything goes smoothly until I add the component listener again. At this point the componentResized method is called about 20 times. It has possibly buffered the resize events from what I can see. Anyone know what's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠魁 2024-09-02 20:56:07

看起来您正在尝试修改事件调度线程 (EDT) 之外的组件。这在 AWT 中甚至都不是一个好主意,更不用说在 Swing 中了。

除了在正确的线程上获取所有 AWT/Swing 内容之外,修复的一部分应该是获取侦听器检查状态以查看它是否应该执行其主体,而不是尝试删除和添加侦听器。

Looks like you are trying to modify a component off the Event Dispatch Thread (EDT). That's not even a good idea in AWT, let alone Swing.

Other than get all the AWT/Swing stuff on the right thread, part of the fix should be to get the listener check state to see if it should execute its body, rather than attempting to remove and add the listener.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文