当前位置: 技术问答>java相关
AWT事件的问题?高分!
来源: 互联网 发布时间:2015-02-16
本文导语: keyPressed(KeyEvent e) 是按下时触发 keyReleased(KeyEvent e) 是松开时触发 keyTyped(KeyEvent e) 是什么时候触发? 好像是输入一个字符时,我弄了半天也没反应? 另外consume()方法的作用是什么?(也没看懂) ...
keyPressed(KeyEvent e) 是按下时触发
keyReleased(KeyEvent e) 是松开时触发
keyTyped(KeyEvent e) 是什么时候触发?
好像是输入一个字符时,我弄了半天也没反应?
另外consume()方法的作用是什么?(也没看懂)
keyReleased(KeyEvent e) 是松开时触发
keyTyped(KeyEvent e) 是什么时候触发?
好像是输入一个字符时,我弄了半天也没反应?
另外consume()方法的作用是什么?(也没看懂)
|
值为0-127ascii码按下就会触发。shift,control键会触发keyPressed,keyReleased但不会触发keyTyped, 可以试试下面程序
import java.awt.*;
import java.awt.event.*;
public class SimpleFrame extends Frame implements ActionListener{
static int counter =0;
TextField tf;
SimpleFrame(){
addWindowListener (new WindowEventHandler());
tf=new TextField(10);
tf.addKeyListener(new KeyHandler());
add(tf);
}
public void actionPerformed(ActionEvent evt) { // handles New button
System.out.println("getActionCommand()="+evt.getActionCommand());
System.out.println("getModifiers()="+evt.getModifiers());
System.out.println("paramString()="+evt.paramString());
System.out.println("getSource()="+evt.getSource());
SimpleFrame f = new SimpleFrame();//就是这里!
counter++;
f.setTitle("Window " + counter);
f.setSize(200, 150);
f.setLocation(30 * counter, 30 * counter);
f.show();
closeAllButton.addActionListener(f);
}
public static void main(String args[]) {
SimpleFrame f = new SimpleFrame();
f.setSize(200, 150);
f.show();
}
}
class WindowEventHandler extends WindowAdapter {
public void windowOpened(WindowEvent e) {
System.out.println("Opened WindowEvent received");
}
public void windowClosing(WindowEvent e) {
System.out.println("Closing WindowEvent received");
e.getWindow().dispose();
}
public void windowClosed(WindowEvent e) {
System.out.println("Closed WindowEvent received");
}
}
class KeyHandler extends KeyAdapter {
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed");
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased");
}
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped");
}
}
import java.awt.*;
import java.awt.event.*;
public class SimpleFrame extends Frame implements ActionListener{
static int counter =0;
TextField tf;
SimpleFrame(){
addWindowListener (new WindowEventHandler());
tf=new TextField(10);
tf.addKeyListener(new KeyHandler());
add(tf);
}
public void actionPerformed(ActionEvent evt) { // handles New button
System.out.println("getActionCommand()="+evt.getActionCommand());
System.out.println("getModifiers()="+evt.getModifiers());
System.out.println("paramString()="+evt.paramString());
System.out.println("getSource()="+evt.getSource());
SimpleFrame f = new SimpleFrame();//就是这里!
counter++;
f.setTitle("Window " + counter);
f.setSize(200, 150);
f.setLocation(30 * counter, 30 * counter);
f.show();
closeAllButton.addActionListener(f);
}
public static void main(String args[]) {
SimpleFrame f = new SimpleFrame();
f.setSize(200, 150);
f.show();
}
}
class WindowEventHandler extends WindowAdapter {
public void windowOpened(WindowEvent e) {
System.out.println("Opened WindowEvent received");
}
public void windowClosing(WindowEvent e) {
System.out.println("Closing WindowEvent received");
e.getWindow().dispose();
}
public void windowClosed(WindowEvent e) {
System.out.println("Closed WindowEvent received");
}
}
class KeyHandler extends KeyAdapter {
public void keyPressed(KeyEvent e) {
System.out.println("keyPressed");
}
public void keyReleased(KeyEvent e) {
System.out.println("keyReleased");
}
public void keyTyped(KeyEvent e) {
System.out.println("keyTyped");
}
}