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");的输出结果,只是将按键简单的显示在文本域中,请问是哪里出现问题了? 展开
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");的输出结果,只是将按键简单的显示在文本域中,请问是哪里出现问题了? 展开
3个回答
展开全部
监听器应该加在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("窗口首次可见");
}
}
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("窗口首次可见");
}
}
展开全部
恩,我的理由和第一个人一样,你的事件实现的方法是在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("窗口首次可见");
}
}
}
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("窗口首次可见");
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
修改一下构造函数:
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加监听器才会起效
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加监听器才会起效
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询