why doesn't the frame close when i press the e

2020-04-07 05:02发布

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 have set a listener for the keys .When ever i press ESC key why doesn't the frame close ?

4条回答
We Are One
2楼-- · 2020-04-07 05:10

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.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-04-07 05:12

The invocation of setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); does not close the frame, it will define the behaviour when the windows decoration [X] close button is pressed (Which you have disabled for full screen). You could replace this with setVisible(false); or exit your programm.

查看更多
The star\"
4楼-- · 2020-04-07 05:25

Use dispose() method.

addKeyListener(new KeyAdapter() {
 public void keyPressed(KeyEvent ke) {  // handler
    if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
      System.out.println("escaped ?");
      displayFullScreen.this.dispose();
      } 
     else {
      System.out.println("not escaped");
      }
     } 
});
查看更多
霸刀☆藐视天下
5楼-- · 2020-04-07 05:29
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);
            }
       });

   }

}
查看更多
登录 后发表回答