看一下这个Java程序,最后为什么会重复输出字母
importjava.awt.*;importjavax.swing.*;importjava.awt.event.*;publicclassXiTi9_7extends...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class XiTi9_7 extends JFrame
{
JTextField text=new JTextField(10);
public static void main(String[] args)
{
XiTi9_7 frm=new XiTi9_7("Keyboard");
frm.setBounds(100,100,200,100);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
XiTi9_7(String s)
{
setTitle(s);
setLayout(new FlowLayout());
add(text);
text.addKeyListener(new MyKey());
}
class MyKey extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
int a=e.getKeyCode();
char ch=e.getKeyChar();
if(ch>='a'&&ch<='z')
text.setText("编码:"+a+" 字母:"+ch);
else
text.setText("只对字母键有效");
}
}
} 展开
import javax.swing.*;
import java.awt.event.*;
public class XiTi9_7 extends JFrame
{
JTextField text=new JTextField(10);
public static void main(String[] args)
{
XiTi9_7 frm=new XiTi9_7("Keyboard");
frm.setBounds(100,100,200,100);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.setVisible(true);
}
XiTi9_7(String s)
{
setTitle(s);
setLayout(new FlowLayout());
add(text);
text.addKeyListener(new MyKey());
}
class MyKey extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
int a=e.getKeyCode();
char ch=e.getKeyChar();
if(ch>='a'&&ch<='z')
text.setText("编码:"+a+" 字母:"+ch);
else
text.setText("只对字母键有效");
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询