简体   繁体   English

Java-如何“取消隐藏” JFrame

[英]Java - How to “unhide” a JFrame

I am quite new to Java, but familiar with native Android dev so bear with me xD. 我对Java还是很陌生,但是熟悉本地Android开发人员,所以请耐心等待xD。 I created an application that creates a JFrame. 我创建了一个创建JFrame的应用程序。 Then I set the closeOperation to: setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 然后我将closeOperation设置为: setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); .

It performs as expected, the frame is hidden and this is what I want (when I close). 它按预期执行,框架被隐藏,这就是我想要的(关闭时)。 I need the application to keep on running (only once instance), because I am running a thread in the background that is performing an operation. 我需要应用程序继续运行(仅一次实例),因为我正在执行操作的后台运行线程。

My actionListener on my button in my JFrame currently does this: setVisible(false); 我在JFrame中按钮上的actionListener当前正在执行此操作: setVisible(false);

My question is this, how can I maximize the JFrame again after it has been hidden? 我的问题是,隐藏JFrame后如何再次最大化JFrame? Would it be possible to display the frame when the user clicks on the minimized application in the task bar? 当用户单击任务栏中的最小化应用程序时,是否可以显示框架? Is there some type of listener that I need to implement? 我需要实现某种类型的侦听器吗?

Thanks in advance, any advice will be appreciated 在此先感谢,任何建议将不胜感激

UPDATE UPDATE

For this solution to work correctly you need to do the following. 为了使该解决方案正常工作,您需要执行以下操作。 Also have a look at XtremeBaumer's answer for this to make sense. 也可以看看XtremeBaumer的答案,使之有意义。

On JFrame creation setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 在创建JFrame时,设置setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); . When you want to minimize the app (on click possibly) frame.setState(Frame.ICONIFIED); 当您想最小化应用程序时(可能单击) frame.setState(Frame.ICONIFIED); . When you want to maximize the app again frame.setState(Frame.NORMAL); 当您想再次最大化应用程序时, frame.setState(Frame.NORMAL); in windowDeiconified event. windowDeiconified事件中。

One last thing, if you want to also minimize your app when the user clicks on the exit button (red x) add this to the windowClosing event frame.setState(Frame.ICONIFIED); 最后一件事,如果您还想在用户单击退出按钮(红色x)时最小化您的应用程序,请将其添加到windowClosing事件frame.setState(Frame.ICONIFIED); .

    this.addWindowListener(new WindowListener(){

        @Override
        public void windowActivated(WindowEvent e) {
        }

        @Override
        public void windowClosed(WindowEvent e) {
        }

        @Override
        public void windowClosing(WindowEvent e) {
            setState(Frame.ICONIFIED)
        }

        @Override
        public void windowDeactivated(WindowEvent e) {
        }

        @Override
        public void windowDeiconified(WindowEvent e) {
            this.setVisible(true);
            //this should be what you want
        }

        @Override
        public void windowIconified(WindowEvent e) {
        }

        @Override
        public void windowOpened(WindowEvent e) {
        }

    });

i hope this solves your question. 我希望这能解决您的问题。 add it to your JFrame 将其添加到您的JFrame

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

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