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

后端 未结 4 1742
走了就别回头了
走了就别回头了 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条回答
  •  春和景丽
    2021-01-18 10:33

    You are not closing your your frame at esc key. You are just setting its default close operation so you must write

    System.exit(0);
    

    or

    dispose();
    

    instead of

    setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    

    If you don't want to exit the application then use setVisible(false).


    Tip:

    VK_ESCAPE is static filed of KeyEvent class so instead of ke.VK_ESCAPE you can write KeyEvent.VK_ESCAPE.

提交回复
热议问题