组合键事件的处理,有一点不明白的地方,帮忙看下,先谢了
import javax.swing.*;import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
class KeyEventFrame extends JFrame implements KeyListener{
JButton btn;
KeyEventFrame(String s){
super(s);
setSize(500,300);
setLocationRelativeTo(null);
JPanel pane=new JPanel();
setContentPane(pane);
pane.setLayout(null);
btn=new JButton("OK");
pane.add(btn);
btn.setBounds(0,0,100,50);
btn.addKeyListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void keyPressed(KeyEvent e){
int width=btn.getWidth(); //得到按钮的宽
int height=btn.getHeight(); //得到按钮的高
int keyCode=e.getKeyCode(); //得到按键虚拟码
if(keyCode==KeyEvent.VK_UP&&e.isControlDown())
btn.setSize(width+2, height+1);
if(keyCode==KeyEvent.VK_DOWN&&e.isControlDown())
btn.setSize(width-1,height-1);
}
public void keyTyped(KeyEvent e){};
public void keyReleased(KeyEvent e){};
}
public class KeyEventDemo {
public static void main(String[] args) {
KeyEventFrame frm=new KeyEventFrame("组合键演示");
}
}
为什么最后还要加 public void keyTyped(KeyEvent e){};
public void keyReleased(KeyEvent e){};
为什么有的不加这些,有的需要加这些,还有是在什么情况下要加的