将JFrame窗口移到最前面 [英] Bring JFrame window to the front

查看:122
本文介绍了将JFrame窗口移到最前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很明显,此问题已经存在此处.然后,在这里,有人对此做出了类似的回答.但是,我尝试了两种方法(正如我所指出的),但似乎都没有用.我使用的是Windows 10上的Java 8 Update 22,而上一篇文章中的发帖人则不是Windows和Java 7上的发布者,因此也许其中之一与它有关.我尝试过的代码,其中 getInstance()是我的JFrame对象:

Obviously, this question already exists here. And over here someone answered it similarly. However, I tried both approaches (as I pointed out) and neither seems to work for me. I'm on Java 8 Update 22 on Windows 10, whereas the poster on that last post is not on Windows and on Java 7, so maybe one of these things has something to do with it. Code I have tried, where getInstance() is my JFrame object:

private static void bringToFront() {                                                             
    getInstance().setVisible(true);                                                              
    getInstance().setExtendedState(JFrame.NORMAL);                                               
    getInstance().toFront();                                                                     
    getInstance().repaint();                                                                     
}

并从此答案中获取.

private static void bringToFront() {
    getInstance().setVisible(true);
    getInstance().toFront();
    getInstance().requestFocus();
    getInstance().repaint();
}

@Override
public void toFront() {
    int sta = super.getExtendedState() & ~JFrame.ICONIFIED & JFrame.NORMAL;

    super.setExtendedState(sta);
    super.setAlwaysOnTop(true);
    super.toFront();
    super.requestFocus();
    super.setAlwaysOnTop(false);
}

请注意,我不希望窗口始终停留在顶部.但是,在最后一个代码段中,它确实排在最前面.

Note that I do not want the window to always stay on top. However, with that last snippet it does stay on top.

我现在意识到它并不总是排在最前面.问题在于该窗口首先显示在顶部,但没有焦点(任务栏中的图标闪烁),这意味着如果用户单击该窗口以外的其他位置,则该窗口将停留在顶部,因为它不在't失去焦点",因为它没有任何开始.我尝试了 requestFocus(),但是那也不起作用.可能是由于此说明.如何确保窗口聚焦于初始弹出窗口?

I now realise it doesn't always stay on top. The problem is that it the window appears on top at first, but doesn't have focus (the icon in the task bar is flickering), meaning that if a user clicks somewhere else than the window the window will stay on top because it isn't "losing focus", as it didn't have any to begin with. I tried requestFocus() but that didn't work either. Probably because of this explanation. How can I make sure the window has focus on initial pop-up?

我尝试了mKorbel的解决方案,但是那也不起作用.仅当该窗口已经出现并已被激活和取消激活,并且在 then 方法被调用时,该窗口才出现在窗口的最前面.但是,首先初始化窗口时,它不会显示在最前面.

I tried mKorbel's solution, but that didn't work either. It works only when the window has already appeared and has been activated, and de-activated and when then the method is called, the window comes to the front. However, when first initialising the window, it doesn't show up to the front.

private static void bringToFront() {
    getInstance().setVisible(true);
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            getInstance().toFront();
        }
    });
    getInstance().requestFocus();
    getInstance().repaint();
}

推荐答案

这是我在下面使用的.

警告: YMMV !我完全不保证它会工作,如果不能,则可能无法在其他操作系统,同一操作系统的不同版本,您的JAVA版本上,或者在Saturn与Jupiter对齐时等.

Warning: YMMV! I don't guarantee at all that it will work and if it does it might not work on another OS, a different version of the same OS, on your version of JAVA, or when Saturn aligns with Jupiter, etc etc.

在这里:

private void hackyToFront( )
{
    // What follows is a hack to make sure that the frame is put to front and activated.
    // Simply calling setVisible( true ) and toFront( ) is not enough.

    SwingUtilities.invokeLater( new Runnable( )
    {
        @Override
        public void run( )
        {
            if( !isVisible( ) )
                setVisible( true );
            setExtendedState( JFrame.NORMAL );
            toFront( );
            setAlwaysOnTop( true );
            try
            {
                final Point oldMouseLocation = MouseInfo.getPointerInfo( ).getLocation( );

                // simulate a mouse click on title bar of window
                Robot robot = new Robot( );
                robot.mouseMove( getX( ) + 100, getY( ) + 10 );
                robot.mousePress( InputEvent.BUTTON1_DOWN_MASK );
                robot.mouseRelease( InputEvent.BUTTON1_DOWN_MASK );

                // move mouse to old location
                robot.mouseMove( (int) oldMouseLocation.getX( ), (int) oldMouseLocation.getY( ) );
            }
            catch( Exception ex )
            {}
            finally
            {
                setAlwaysOnTop( false );
            }
        }
    } );
}

这篇关于将JFrame窗口移到最前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆