Why doesn't the frame close when I press the evacuation key?

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;


public class displayFullScreen extends JFrame {
        private JLabel alarmMessage = new JLabel("Alarm !");
        private JPanel panel = new JPanel();
        public displayFullScreen() {
            setUndecorated(true);
            panel.setLayout(new FlowLayout(FlowLayout.CENTER));
            alarmMessage.setText("Alarm !");
            alarmMessage.setFont(new Font("Cambria",Font.BOLD,100));
            alarmMessage.setForeground(Color.CYAN);
            panel.add(alarmMessage);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds(0,0,screenSize.width,screenSize.height);
            panel.setBackground(Color.black);
            add(panel);

            addKeyListener(new KeyAdapter() {
               public void keyPressed(KeyEvent ke) {  // handler
        if(ke.getKeyCode() == ke.VK_ESCAPE) {
                       System.out.println("escaped ?");
                       setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); // trying to close
                    } else {
                       System.out.println("not escaped");
                     }
              } 
           });
        }          

        public static void main(String args[]) {
    new displayFullScreen().setVisible(true);
    }

}

I installed a key listener. When I press a key ESC, why doesn't the frame close?

+5
source share
4 answers

The call setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);does not close the frame, it will determine the behavior when you click the close button of the window [X] (which you disabled for full screen). You can replace this with setVisible(false);or exit the program.

+8
source

Use the method dispose().

addKeyListener(new KeyAdapter() {
 public void keyPressed(KeyEvent ke) {  // handler
    if(ke.getKeyCode() == ke.VK_ESCAPE) {
      System.out.println("escaped ?");
      displayFullScreen.this.dispose();
      } 
     else {
      System.out.println("not escaped");
      }
     } 
});
+7
source
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);
            }
       });

   }

}
+5

esc. ,

System.exit(0);

dispose();

setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

, setVisible(false).


:

VK_ESCAPEis statically placed in the class KeyEvent, so instead ke.VK_ESCAPEyou can write KeyEvent.VK_ESCAPE.

+3
source

All Articles