当前位置: 技术问答>java相关
一个小问题?教教初学者吧!给分
来源: 互联网 发布时间:2015-02-06
本文导语: import java.awt.*; import java.awt.event.*; public class KeyEventTest extends Frame { private TextArea msgArea; public static void main(String args[]) { KeyEventTest test=new KeyEventTest(); test.setSize(300,300); test.setVisible(true); } public Ke...
import java.awt.*;
import java.awt.event.*;
public class KeyEventTest extends Frame {
private TextArea msgArea;
public static void main(String args[]) {
KeyEventTest test=new KeyEventTest();
test.setSize(300,300);
test.setVisible(true);
}
public KeyEventTest() {
msgArea=new TextArea();
msgArea.setEnabled(false);//让Button取得控制权
add(msgArea,BorderLayout.CENTER);
Button b=new Button("test");
b.addKeyListener(new KeyListen(this));//让Button能够处理KeyEvent
add(b,BorderLayout.SOUTH);
}
public void ShowMessage(String str) {
msgArea.append(str+"n");
}
}
class KeyListen implements KeyListener {//用来接收KeyEvent的类
private KeyEventTest parent;
public KeyListen(KeyEventTest p) {
parent=p;
}
public void KeyPressed(KeyEvent e) {
}
public void KeyReleased(KeyEvent e) {
parent.ShowMessage("Key char=" + e.getKeyChar());
parent.ShowMessage("Key code=" + e.getKeyCode());
parent.ShowMessage("modifier=" + e.getModifiers());
}
public void KeyTyped(KeyEvent e) {
}
}
这个程序该怎样改正确?我使用的是JDK帮忙解决给分!!
import java.awt.event.*;
public class KeyEventTest extends Frame {
private TextArea msgArea;
public static void main(String args[]) {
KeyEventTest test=new KeyEventTest();
test.setSize(300,300);
test.setVisible(true);
}
public KeyEventTest() {
msgArea=new TextArea();
msgArea.setEnabled(false);//让Button取得控制权
add(msgArea,BorderLayout.CENTER);
Button b=new Button("test");
b.addKeyListener(new KeyListen(this));//让Button能够处理KeyEvent
add(b,BorderLayout.SOUTH);
}
public void ShowMessage(String str) {
msgArea.append(str+"n");
}
}
class KeyListen implements KeyListener {//用来接收KeyEvent的类
private KeyEventTest parent;
public KeyListen(KeyEventTest p) {
parent=p;
}
public void KeyPressed(KeyEvent e) {
}
public void KeyReleased(KeyEvent e) {
parent.ShowMessage("Key char=" + e.getKeyChar());
parent.ShowMessage("Key code=" + e.getKeyCode());
parent.ShowMessage("modifier=" + e.getModifiers());
}
public void KeyTyped(KeyEvent e) {
}
}
这个程序该怎样改正确?我使用的是JDK帮忙解决给分!!
|
import java.awt.*;
import java.awt.event.*;
public class KeyEventTest extends Frame {
private TextArea msgArea;
public static void main(String args[]) {
KeyEventTest test=new KeyEventTest();
test.setSize(300,300);
test.setVisible(true);
}
public KeyEventTest() {
msgArea=new TextArea();
msgArea.setEnabled(false);//让Button取得控制权
add(msgArea,BorderLayout.CENTER);
Button b=new Button("test");
b.addKeyListener(new KeyListen(this));//让Button能够处理KeyEvent
add(b,BorderLayout.SOUTH);
}
public void ShowMessage(String str) {
msgArea.append(str+"n");
}
}
class KeyListen implements KeyListener {//用来接收KeyEvent的类
private KeyEventTest parent;
public KeyListen(KeyEventTest p) {
parent=p;
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
parent.ShowMessage("Key char=" + e.getKeyChar());
parent.ShowMessage("Key code=" + e.getKeyCode());
parent.ShowMessage("modifier=" + e.getModifiers());
}
public void keyTyped(KeyEvent e) {
}
}
你那三个事件,keyTyped什么的写成KeyTyped了,大小写错了,看看编译器提示吧
import java.awt.event.*;
public class KeyEventTest extends Frame {
private TextArea msgArea;
public static void main(String args[]) {
KeyEventTest test=new KeyEventTest();
test.setSize(300,300);
test.setVisible(true);
}
public KeyEventTest() {
msgArea=new TextArea();
msgArea.setEnabled(false);//让Button取得控制权
add(msgArea,BorderLayout.CENTER);
Button b=new Button("test");
b.addKeyListener(new KeyListen(this));//让Button能够处理KeyEvent
add(b,BorderLayout.SOUTH);
}
public void ShowMessage(String str) {
msgArea.append(str+"n");
}
}
class KeyListen implements KeyListener {//用来接收KeyEvent的类
private KeyEventTest parent;
public KeyListen(KeyEventTest p) {
parent=p;
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
parent.ShowMessage("Key char=" + e.getKeyChar());
parent.ShowMessage("Key code=" + e.getKeyCode());
parent.ShowMessage("modifier=" + e.getModifiers());
}
public void keyTyped(KeyEvent e) {
}
}
你那三个事件,keyTyped什么的写成KeyTyped了,大小写错了,看看编译器提示吧