我不知道为什么KeyListener不起作用。 如果用户按下" Shift",程序应该关闭。
public class Main extends JFrame implements KeyListener{
public static void main(String[] args){
new Main();
}
public Main(){
JFrame guiFrame = new JFrame();
//make sure the program exits when the frame closes
guiFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
guiFrame.setLayout(new FlowLayout());
guiFrame.setTitle("");
guiFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
guiFrame.setUndecorated(true);
guiFrame.setAlwaysOnTop(true);
guiFrame.getContentPane().setBackground(Color.black);
JLabel jb1 = new JLabel("WER DAS LIEST IST BLÖD",JLabel.CENTER);
jb1.setFont(new Font("Serif", Font.BOLD, 140));
jb1.setForeground(Color.WHITE);
jb1.setLocation((guiFrame.getWidth()-jb1.getWidth())/2,50);
guiFrame.add(jb1);
//This will center the JFrame in the middle of the screen
guiFrame.setLocationRelativeTo(null);
guiFrame.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_SPACE){
System.exit(0);
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:4)
仅仅实现array[num] = '\0';
界面是不够的 - 您需要将其添加到某个组件(在您的情况下是KeyListener
)。
尝试类似
的内容JFrame
这里有一个官方教程: https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html