请看下面的代码
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class KeyCheck extends JFrame
{
private JButton check;
private JPanel panel;
private FlowLayout flow;
public KeyCheck()
{
check = new JButton("Check");
check.addKeyListener(new KeyWork());
panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(check);
getContentPane().add(panel);
}
private class KeyWork extends KeyAdapter
{
public void keyPressed(KeyEvent k)
{
if(k.getKeyCode()==KeyEvent.VK_CONTROL && KeyEvent.VK_A)
{
JOptionPane.showMessageDialog(null, "OK");
}
}
}
public static void main(String[]args)
{
KeyCheck k = new KeyCheck();
k.setVisible(true);
k.setSize(200,200);
k.validate();
k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
在这种情况下,我向按钮添加了一个按键列表,当同时按下 CTRL + A(控制键和“A”键)时,我需要它来显示消息“OK”。但是上面的代码是错误的。所以,当两个键同时按下时,请帮我收到消息。