自己写了段代码,可是执行后发现 按enter键没有效果 这是咋回事啊。我的光标控制肯定是在Button上
- import java.awt.*;
- import java.awt.event.*;
- public class Demo_Awt {
- private Frame f;
- private Button but;//按钮
-
- Demo_Awt() {
- init();
- }
- public void init()
- {
- f = new Frame("My Frame");
- f.setBounds(300, 100, 500, 600);
- f.setLayout(new FlowLayout());
- but = new Button("My Button");
- //将组件添加到Frame中
- f.add(but);
- //调用监听
- myEvent();
- f.setVisible(true);
- }
- private void myEvent() {
- //匿名内部类的写法
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {
- System.exit(0);
- }
- });
- but.addKeyListener(new KeyAdapter()
- {
- public void keyPerssed(KeyEvent e)
- {
- //监听判断当键盘按键ENTER时触发
- if(e.getKeyCode()==KeyEvent.VK_ENTER)
- System.exit(0);
- }
- });
- }
- public static void main(String[] args) {
- new Demo_Awt();
- }
- }
复制代码 |
|