why doesn't the frame close when i press the escape key?

后端 未结 4 1718
走了就别回头了
走了就别回头了 2021-01-18 09:53
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class displayFullScreen extends JFrame {
        private JLabel alarmMessage = new JLabel(         


        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-18 10:43

    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import javax.swing.AbstractAction;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.KeyStroke;
    
    public abstract class EscapableFrame extends JFrame
    {
        public EscapableFrame()
        {
            // on ESC key close frame
            getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
            KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Cancel"); //$NON-NLS-1$
            getRootPane().getActionMap().put("Cancel", new AbstractAction()
            { 
    
                public void actionPerformed(ActionEvent e)
                {
                    System.exit(0);
                    //framename.setVisible(false);
                }
           });
    
       }
    
    }
    

提交回复
热议问题