gpt4 book ai didi

java - 为什么当我按退出键时框架不关闭?

转载 作者:行者123 更新时间:2023-12-01 21:26:31 25 4
gpt4 key购买 nike

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);
}

}

我已经为按键设置了监听器。当我按下 ESC 键时,为什么框架不关闭?

最佳答案

调用 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 不会关闭框架,它将定义按下窗口装饰 [X] 关闭按钮(您已禁用全屏)时的行为)。您可以将其替换为 setVisible(false); 或退出程序。

关于java - 为什么当我按退出键时框架不关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58830696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com