Below is a program that displays a black screen with an alarm notification!
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class displayFullScreen extends Window {
private JLabel alarmMessage = new JLabel("Alarm !");
public displayFullScreen() {
super(new JFrame());
setLayout(new FlowLayout(FlowLayout.CENTER));
alarmMessage.setFont(new Font("Cambria",Font.BOLD,100));
alarmMessage.setForeground(Color.CYAN);
add(alarmMessage);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width,screenSize.height);
setBackground(Color.black);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent ke) {
escapeHandler(ke);
}
});
}
public void escapeHandler(KeyEvent ke) {
if(ke.getKeyCode() == ke.VK_ESCAPE) {
System.out.println("escaped !");
} else {
System.out.println("Not escaped !");
}
}
public static void main(String args[]) {
new displayFullScreen().setVisible(true);
}
}
I installed a key handler in this program. The handler captures the pressed keys when the focus is in the window. When the escape key is pressed, escaped !it should be displayed differently !escaped. But nothing is displayed when I press a key. What is the problem?
source
share