
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不了.. 展开
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不了.. 展开
2个回答
展开全部
直接上代码
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少写了一个接口中的方法
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少写了一个接口中的方法
展开全部
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之路更有趣,有什么问题欢迎讨论。
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之路更有趣,有什么问题欢迎讨论。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询