简体   繁体   中英

Why setExtendedState(JFrame.ICONIFIED)does not work while windows screen locked?

all. i want to minimize my jframe with setExtendedState(JFrame.ICONIFIED), in most case, it works properly, but when it does not work when i lock my os(windows XP) screen with WIN+L.My wimple code are as follows:

import javax.swing.JDialog;
import javax.swing.JFrame;

public class FrameTest extends JFrame {
    public static FrameTest ft = new FrameTest();

    public static void main(String[] args)
    {
        FrameTest.ft.setVisible(true);
        FrameTest.ft.setLocation(300, 300);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JDialog dlg = new JDialog( ft, "xxx", true );
        ft.setExtendedState(JFrame.ICONIFIED);
        dlg.setVisible(true);//if not have this line, it works also in screen lock case 
    }   
}

Any help will be appreciated.

It could me that you are manipulating Swing components from the main thread instead of the Event Dispatch Thread. Try wrapping the contents of main in:

public static void main(String[] args)
{
SwingUtilities.invokeLater(new Rennable() {
    @Override
    void run() {
        FrameTest.ft.setVisible(true);
        FrameTest.ft.setLocation(300, 300);
    }
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    SwingUtilities.invokeLater(new Rennable() {
        @Override
        void run() {
            JDialog dlg = new JDialog( ft, "xxx", true );
            ft.setExtendedState(JFrame.ICONIFIED);
            dlg.setVisible(true);case 
     }   
}

If that doesn't help, try splitting the second invokeLater block into:

    SwingUtilities.invokeLater(new Rennable() {
        @Override
        void run() {
            ft.setExtendedState(JFrame.ICONIFIED);
     }   
    SwingUtilities.invokeLater(new Rennable() {
        @Override
        void run() {
            JDialog dlg = new JDialog( ft, "xxx", true );
            dlg.setVisible(true);case 
     }   

That gives Swing a chance to respond to the iconification before handing off control to the dialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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