我编写了一个测试按键的小程序,如下:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassKeyinEventextendsJFrameimplementsKeyListener{JTextAreatextarea=...
我编写了一个测试按键的小程序,如下:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class KeyinEvent extends JFrame implements KeyListener
{
JTextArea textarea=new JTextArea(4,20);
Container c;
public KeyinEvent()
{
super("按键测试");
c=getContentPane();
c.setLayout(new FlowLayout());
c.add(textarea);
//textarea.setText("请按任意键");
addKeyListener(this);
setSize(300,120);
setVisible(true);
//addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
String line1="按键"+e.getKeyChar();
String line2="按键是否为Action键"+e.isActionKey();
String mkey=e.getKeyModifiersText(e.getModifiers());
String line3="是否按下Modifier键"+(mkey.equals("")?"否":"是,其键为:"+mkey);
textarea.setText(line1+"\n"+line2+"\n"+line3+"\n");
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args)
{
KeyinEvent be=new KeyinEvent();
be.addWindowListener(new MyWindowListener());
}
}
其中MyWindowListener这个类如下:
import java.awt.event.*;
public class MyWindowListener implements WindowListener
{
public void windowActivated(WindowEvent e)
{
System.out.println("窗口为活动状态");
}
public void windowClosed(WindowEvent e)
{
System.out.println("窗口为关闭状态");
System.exit(0);
}
public void windowClosing(WindowEvent e)
{
System.out.println("窗口正在关闭");
System.exit(0);
}
public void windowDeactivated(WindowEvent e)
{
System.out.println("窗口不再活动");
}
public void windowDeiconified(WindowEvent e)
{
System.out.println("窗口由最小化变为正常");
}
public void windowIconified(WindowEvent e)
{
System.out.println("窗口为最小化");
}
public void windowOpened(WindowEvent e)
{
System.out.println("窗口首次可见");
}
}
我在运行KeyinEvent这个类时得不到我想要的即textarea.setText(line1+"\n"+line2+"\n"+line3+"\n");的输出结果,只是将按键简单的显示在文本域中,请问是哪里出现问题了?
展开