简体   繁体   English

Swing Java,使用菜单时关闭特定窗口

[英]Swing java, close specific window when menu is used

I have a class which creates a menu with all of the menuItems etc, I then have many UIs which have a JMenu in which I give the JMenu from the first class. 我有一个使用所有menuItems等创建菜单的类,然后有许多UI包含JMenu,在其中我从第一个类给出了JMenu。 My problem is that the menu has many items which open other UIs, how do I make a check so that one is opened, the current window is closed? 我的问题是菜单上有很多项目可以打开其他UI,如何进行检查以便打开一个UI,关闭当前窗口?

A very short version of what I do is : 我做的一个非常简短的版本是:

Menu: 菜单:

public Menu()
{       
    menuBar = new JMenuBar();
    }
    public JMenuBar makeMenu()
    {
         return menuBar;
     }

UI : 用户界面:

public UI()
{
 JMenuBar menu = Menu.makeMenu();
 }

Note this is just to see the idea, the code is far more complicated but would be useless to show. 请注意,这只是为了看清想法,代码要复杂得多,但是显示起来毫无用处。 The reason why I do this is that I have such a gigantic menu that it would add a huge amount of repeatable code to all UIs. 之所以这样做,是因为我有一个巨大的菜单,它将为所有UI添加大量可重复的代码。

假设每个窗口都有自己的菜单栏,则可以使用“ 关闭应用程序”中ExitAction类。

Using the code that camickr provided, I make a perfect fix for this problem by just turning the visibility off of the first frame while the other one loads, this way the system doesn t close since ExitAction code was closing before anything else appeared. 使用camickr提供的代码,我通过关闭第一帧的可见性而在另一帧加载时关闭了可视性,从而完美解决了此问题,因为ExitAction代码在出现任何其他内容之前就已关闭,因此系统无法关闭。 The code is as it follows : 代码如下:

for (Window frame : Window.getWindows())
        {
            if (frame.isActive())
            {
                frame.setVisible(false);
            }
        }

Hope it will help people 希望对大家有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM