Java KeyListener

importjava.awt.*;importjava.awt.event.*;publicclassMainClassextendsFrame{privateKeyEv... import java.awt.*;
import java.awt.event.*;
public class MainClass extends Frame
{
private KeyEventHandler keyListener = new KeyEventHandler ();
private WindowCloser windowListener = new WindowCloser ();
public MainClass ()
{
addKeyListener (keyListener);
addWindowListener (windowListener);
setSize (200, 200);
setVisible (true);
}

public static void main (String[] args)
{
MainClass a = new MainClass ();
} // main method
}
class KeyEventHandler implements KeyListener
{
public void KeyPressed (KeyEvent ke)
{
if (ke.getKeyChar () == 'q')
{
System.exit (0);
}
}

public void keyReleased (KeyEvent ke)
{
}

public void keyTyped (KeyEvent ke)
{
System.out.println (ke.getKeyChar ());
}
}
class WindowCloser implements WindowListener
{
public void windowClosing (WindowEvent we)
{
System.exit (0);
}

public void windowOpened (WindowEvent we)
{
}

public void windowIconified (WindowEvent we)
{
}

public void windowDeiconified (WindowEvent we)
{
}

public void windowClosed (WindowEvent we)
{
}

public void windowActivated (WindowEvent we)
{
}

public void windowDeactivated (WindowEvent we)
{
}
}

哪位高手帮忙看看哪错了。。
回二楼..不好意思,这是test的.. 后面的复杂着呢...
Ready to program..你的run不了..
展开
 我来答
荧光小草
2010-05-20 · TA获得超过317个赞
知道小有建树答主
回答量:285
采纳率:0%
帮助的人:172万
展开全部
直接上代码
import java.awt.*;
import java.awt.event.*;
public class MainClass extends Frame
{
private KeyEventHandler keyListener = new KeyEventHandler ();
private WindowCloser windowListener = new WindowCloser ();
public MainClass ()
{
addKeyListener (keyListener);
addWindowListener (windowListener);
setSize (200, 200);
setVisible (true);
}

public static void main (String[] args)
{
MainClass a = new MainClass ();
} // main method
}
class KeyEventHandler implements KeyListener
{
public void KeyPressed (KeyEvent ke)
{
if (ke.getKeyChar () == 'q')
{
System.exit (0);
}
}

public void keyReleased (KeyEvent ke)
{
}

public void keyTyped (KeyEvent ke)
{
System.out.println (ke.getKeyChar ());
}

public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub

}
}
class WindowCloser implements WindowListener
{
public void windowClosing (WindowEvent we)
{
System.exit (0);
}

public void windowOpened (WindowEvent we)
{
}

public void windowIconified (WindowEvent we)
{
}

public void windowDeiconified (WindowEvent we)
{
}

public void windowClosed (WindowEvent we)
{
}

public void windowActivated (WindowEvent we)
{
}

public void windowDeactivated (WindowEvent we)
{
}
}
你在这一个类中class KeyEventHandler implements KeyListener少写了一个接口中的方法
百度网友b5880be
2010-05-20 · TA获得超过689个赞
知道小有建树答主
回答量:164
采纳率:0%
帮助的人:198万
展开全部
public class Test extends JFrame{
public Test(){
this.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
System.out.println(e.getKeyCode());
}
});
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.out.println("窗口已关闭!");
}
});
this.setBounds(300, 200, 200, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Test();
}
}
你累不累啊?你要用什么方法就直接用适配器去用该方法,何必全部实现?看看我的例子吧,以后别这样写了,要被人笑话的 ^_^
愿你的JAVA之路更有趣,有什么问题欢迎讨论。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式