简体   繁体   中英

How to disable Window+D in Swing application?

I have a swing application which is in Full Screen mode. I have added a button to quit my application. Now i need to disable WINDOW+D option

I had tried using KeyEventListener to disable to D but i am not getting expected result.

if (e.getKeyCode()==KeyEvent.VK_D){
}

For disabling ALT+TAB i have used below code which works fine.

frame.setAlwaysOnTop(true);

Any Suggestions?

For that purpose you can try to use WindowAdapter method windowIconified() :

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class TestFrame extends JFrame {

    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setVisible(true);
    }

    private void init() {
        TestFrame.this.setExtendedState(JFrame.MAXIMIZED_BOTH);
        add(new JLabel("test"));
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowIconified(WindowEvent e) {
                System.out.println("cant minimize");
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        TestFrame.this.setExtendedState(JFrame.MAXIMIZED_BOTH);
                    }
                });
            }
        });
    }

    public static void main(String... strings) {
        new TestFrame();
    }

}

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