JAVA语言中KeyListener接口的使用,即keyPressed、keyTyped和keyReleased三种方法的按键应该是如何操作的

我编写了一个测试按键的小程序,如下:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicc... 我编写了一个测试按键的小程序,如下:
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");的输出结果,只是将按键简单的显示在文本域中,请问是哪里出现问题了?
展开
 我来答
方杭尽
推荐于2017-10-08 · TA获得超过1万个赞
知道大有可为答主
回答量:1296
采纳率:0%
帮助的人:655万
展开全部
监听器应该加在JTextArea上,而不是窗口上:

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());
textarea.addKeyListener(this);
c.add(textarea);
//textarea.setText("请按任意键");
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());
}
}

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("窗口首次可见");
}
}
航7590944437
2010-09-28
知道答主
回答量:14
采纳率:0%
帮助的人:0
展开全部
恩,我的理由和第一个人一样,你的事件实现的方法是在textarea上,而不是在窗体上.

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("请按任意键");
textarea.addKeyListener(this); //
addWindowListener(new MyWindowListener());
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)
{
new KeyinEvent();
}

class MyWindowListener implements WindowListener
{
public void windowActivated(WindowEvent e)
{
System.out.println("窗口为活动状态");
}

public void windowClosed(WindowEvent e)
{
System.out.println("窗口为关闭状态");
}

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("窗口首次可见");
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zero0087
2010-09-28 · TA获得超过102个赞
知道小有建树答主
回答量:99
采纳率:0%
帮助的人:101万
展开全部
修改一下构造函数:
public KeyinEvent()
{
super("按键测试");
c=getContentPane();
c.setLayout(new FlowLayout());
c.add(textarea);
textarea.addKeyListener(new KeyListener(){

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){}

});
//textarea.setText("请按任意键");
// addKeyListener(this);
setSize(300,120);
setVisible(true);

//addKeyListener(this);
}

运行是光标定位到testarea上,所以要在testarea加监听器才会起效
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式