和KeyPressed有关的这个程序出错了?

huyanqieyu 2008-03-21 07:42:56
import java.awt.event.*;
import java.awt.*;

public class ww {
public static void main(String[] args) {
Button b = new Button("Press Me!");
b.addKeyListener(new KeyHandler());
Frame f = new Frame("MyFrame");
f.add(b);
f.setVisible(true);
}
}

class KeyHandler implements KeyListener {

public void keyPressed(KeyEvent e)
{
System.out.println("Action occurred");
}
}
...全文
66 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
quanc_java 2008-03-21
  • 打赏
  • 举报
回复
import java.awt.event.*;
import java.awt.*;

public class ww {
public static void main(String[] args) {

Frame f = new Frame("MyFrame");
f.setSize(100,100);
Button b = new Button("Press Me!");
b.addKeyListener(new KeyHandler());
f.add(b);

//关闭窗口
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setVisible(true);
}
}

class KeyHandler implements KeyListener {

public void keyPressed(KeyEvent e)
{
System.out.println("Action occurred");
}
public void keyTyped(KeyEvent e){

}
public void keyReleased(KeyEvent e){

}
}
因为你所实现的接口KeyListener中定义了三个方法keyPressed(KeyEvent e) ,keyTyped(KeyEvent e),keyReleased(KeyEvent e),所以你在具体让一个类实现此接口的时候,你必须得实现其中的这些方法.还有因为keyListener是处理键盘事件的,不知你用个Button按钮是什么意思. 你可以用处理鼠标事件的接口MouseListener

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧