How to turn off visibility of X button and disable alt + F4 function

前端 未结 2 1917
抹茶落季
抹茶落季 2021-01-24 17:41

Can someone tell me how to set my X button in main application window visible to false and how to set Alt + F4 function not available or just disable it?


Update

相关标签:
2条回答
  • 2021-01-24 18:14

    For setting X button to invisible is very much described by @trashgod and For disabling your ALT + F4 thing, you can simply write frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

    or you can addWindowListener(...) to your JFrame like this :

    Code snippet to show what i am saying :

    import java.awt.event.*;
    import javax.swing.*;
    
    public class FrameTest
    {
        private WindowAdapter windowAction;
        private JFrame frame;
    
        public FrameTest()
        {
            frame = new JFrame("FRAME TEST");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationByPlatform(true);
    
            windowAction = new WindowAdapter()
            {
                public void windowClosing(WindowEvent we)
                {
                    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                }
            };
    
            frame.addWindowListener(windowAction);
    
            frame.setSize(100, 100);
            frame.setVisible(true);     
        }
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new FrameTest();
                }
            });
        }
    }
    
    0 讨论(0)
  • 2021-01-24 18:16

    "A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated."—Frame

    Addendum: You can send a WINDOW_CLOSING event and bind that Action to your desired Keystroke, as shown here.

    0 讨论(0)
提交回复
热议问题