请大家帮忙看看,这段代码要怎么修改才行呢?
import java.awt.*;
import java.awt.event.*;
public class S extends Frame{
private KeyEventHandler keyListener= new KeyEventHandler();
private WindowCloser windowListener= new WindowCloser();
public S(){
addKeyListener(keyListener);
addWindowListener(windowListener);
setSize(200,200);
setVisible(true);
}
public static void main(String args[])
{S p= new S();}
}
public class KeyEventHandler implements KeyListener{
/**
*
* @param ke
*/
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("Key Listener:Key pressed:"+ ke.getKeyChar());}
}
public 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){}
}
import java.awt.*;
import java.awt.event.*;
public class S extends Frame{
private KeyEventHandler keyListener= new KeyEventHandler();
private WindowCloser windowListener= new WindowCloser();
public S(){
addKeyListener(keyListener);
addWindowListener(windowListener);
setSize(200,200);
setVisible(true);
}
public static void main(String args[])
{S p= new S();}
}
public class KeyEventHandler implements KeyListener{
/**
*
* @param ke
*/
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("Key Listener:Key pressed:"+ ke.getKeyChar());}
}
public 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){}
}
解决方案 »
- 用editplus写的java程序如何打包成安装程序?
- java nettbeans 编程问题,输入时以空格区分两个数字结果不正确,以enter则结果正确。困惑中啊,另外有什么写得不好的地方也请指出,谢谢啊
- jsp文件是否存在问题
- shell调用java的问题:java如何获得shell的输入
- JComboBox无法动态改变
- 急求一个包
- Java 用Netbeans 和Eclipse开发工具引入jar文件
- JComboBox 自动提示功能问题!!
- 键盘事件问题,老大们帮帮忙!!!!
- 各位用过JMF的老兄,当播放网络上的视频文件时,会在临时文件夹中拼命的生成文件,怎么办?
- 一道面试题,大家和我一起做
- Which three will compile and rim without exception?
import java.awt.*;
import java.awt.event.*;class S extends Frame { private KeyEventHandler keyListener = new KeyEventHandler();
private WindowCloser windowListener = new WindowCloser(); public S() {
addKeyListener(keyListener);
addWindowListener(windowListener);
setSize(200, 200);
setVisible(true);
} public static void main(String args[]) {
S p = new S();
}
}class KeyEventHandler implements KeyListener { /**
*
* @param ke
*/
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("Key Listener:Key pressed:" + 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) {
}
}
帮你顶下..
/**
*
* @param ke
*/
是什么意思呢?小弟刚学java不久,还希望能帮忙解释下?谢谢
import java.awt.*;
import java.awt.event.*;class S extends Frame { public S() {
addKeyListener(new KeyAdapter() { @Override
public void keyPressed(KeyEvent ke) {
if (ke.getKeyChar() == 'q') {
System.exit(0);
}
} @Override
public void keyTyped(KeyEvent ke) {
System.out.println("Key Listener:Key pressed:" + ke.getKeyChar());
}
});
addWindowListener(new WindowAdapter() { @Override
public void windowClosing(WindowEvent e) {
S.this.dispose();
System.exit(0);
}
});
setSize(200, 200);
setVisible(true);
} public static void main(String args[]) {
S p = new S();
}
}