在 Java Swing 中销毁 JPopupMenu

发布于 2024-10-30 17:53:15 字数 2224 浏览 5 评论 0原文

我使用的是当我右键单击 GUI 中的某些组件时显示的 JPopupMenu。

现在我必须销毁在这两种情况下显示的弹出菜单:

  1. 用户单击弹出窗口中显示的菜单条目(执行相关操作并关闭弹出窗口)
  2. 用户单击屏幕上的其他位置(关闭弹出窗口而不执行任何操作)

我解决了这个问题,将当前可见的弹出窗口存储到 ArrayList 中,并在发生前两种情况之一时手动将它们设置为不可见。

所以,我想知道两件事:

  1. 有没有更干净的方法可以做到这一点,而无需手动获取所有活动弹出窗口的引用? (也许任何 Swing 功能都可以实现这一点?)
  2. 是否足以将弹出窗口设置为不可见,不再引用该对象,以便释放其分配的内存?或者我必须使用像 dispose 这样的方法? (JPopupMenu 中没有定义 dispose 方法)

显示我的实际代码有点困难,因为它有点复杂。无论如何,它执行以下操作:

public EditorPopupMenu getPopupMenu() {
    this.popupMenu = new EditorPopupMenu();

    EditorMenuItem copy = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().copyItemAction);
    EditorMenuItem cut = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().cutItemAction);
    EditorMenuItem paste = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().pasteItemAction);

    this.popupMenu.add(copy);
    this.popupMenu.add(cut);
    this.popupMenu.add(paste);

    this.popupMenu.addSeparator();

    EditorMenuItem settings = GuiConcreteFactory.getInstance().createMenuItem(
                                                new ApplicationShowDialogAction("settings",null, 
                                                        new EditorAreaDialog (this)) );
    this.popupMenu.add(settings);
    return popupMenu;
}

其中 EditorPopupMenu 扩展了 JPopupMenu。当在特定对象上发生单击并且指定对象构造其弹出菜单并返回它时,MouseListener 会调用前面的代码。

从 MouseListener 内部:

if (me.getModifiers() == InputEvent.BUTTON3_MASK){
                // //System.out.println("ResizableMouseAdapter: BUTTON_3_MASK");


                 EditorPopupMenu popupMenu = sourceComp.getType().getPopupMenu();
                 if ( popupMenu!= null){
                     //System.out.println("COMPONENT HAS A POPUP MENU");
                     popupMenu.setLocation( sourceComp.getLocationOnScreen().x + me.getX(),
                                            sourceComp.getLocationOnScreen().y + me.getY());
                     popupMenu.setVisible(true);
                     Gui.getInstance().addActivePopup(popupMenu);
                 }

             }

这就是全部。使用此代码我的 JPopupMenu 不会正确消失。

I'm using a JPopupMenu displayed when I do a right click on certain components in my GUI.

Now I have to destroy the popup menu displayed in this 2 situation:

  1. The user click on a menu entry displayed into the popup (do the related action and close the popup)
  2. The user click somewhere else on the screen(close the popup without do anything)

I solved this problem storing into an ArrayList the current visible popups and I manually set them to be invisible when one of the 2 previous situation occured.

So, i would like to know 2 things:

  1. Is there any cleaner way of doing that without manually taking the reference of all active popups? (perhaps any Swing feature do accomplish that? )
  2. Is just enough to set a popup unvisible having no more references to that object, in order to free its allocated memory? Or have I to use a method like dispose ? (there isn't a dispose method defined in JPopupMenu)

It is a bit difficult to show my actual code, because it's a bit complex. Anyway it does the following:

public EditorPopupMenu getPopupMenu() {
    this.popupMenu = new EditorPopupMenu();

    EditorMenuItem copy = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().copyItemAction);
    EditorMenuItem cut = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().cutItemAction);
    EditorMenuItem paste = GuiConcreteFactory.getInstance().createMenuItem(Gui.getInstance().pasteItemAction);

    this.popupMenu.add(copy);
    this.popupMenu.add(cut);
    this.popupMenu.add(paste);

    this.popupMenu.addSeparator();

    EditorMenuItem settings = GuiConcreteFactory.getInstance().createMenuItem(
                                                new ApplicationShowDialogAction("settings",null, 
                                                        new EditorAreaDialog (this)) );
    this.popupMenu.add(settings);
    return popupMenu;
}

Where EditorPopupMenu extends JPopupMenu. Previous code is called by a MouseListener when a click happend on a particular object and the specified object constructs its popup menu and returns it.

From inside the MouseListener:

if (me.getModifiers() == InputEvent.BUTTON3_MASK){
                // //System.out.println("ResizableMouseAdapter: BUTTON_3_MASK");


                 EditorPopupMenu popupMenu = sourceComp.getType().getPopupMenu();
                 if ( popupMenu!= null){
                     //System.out.println("COMPONENT HAS A POPUP MENU");
                     popupMenu.setLocation( sourceComp.getLocationOnScreen().x + me.getX(),
                                            sourceComp.getLocationOnScreen().y + me.getY());
                     popupMenu.setVisible(true);
                     Gui.getInstance().addActivePopup(popupMenu);
                 }

             }

This is all. With this code my JPopupMenu doesn't dissapear properly.

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

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

发布评论

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

评论(2

逆蝶 2024-11-06 17:53:15

这是默认行为。

阅读 Swing 教程中关于调出弹出菜单 获取解释和工作示例。

This is the default behaviour.

Read the section from the Swing tutorial on Bringing Up a Popup Menu for an explanation and working example.

双马尾 2024-11-06 17:53:15

使用 show 方法而不是 setVisible 方法。

Use the show method instead of the setVisible method.

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