题目内容
本题的功能是监听键盘敲击事件,并将敲击的字符显示在标签上。开始,文字标签提示“Please press your keyboard!”,当按下键盘上的字符键,文字标签就变为“*ispressed!”(*为所按字母)。 import java.awt.*; import java.awt.event*’; import javax.swing.*; public class java2 extends Frame { Dublic static void main(String args[]){ java2 f=new java2("java2"); Panel pan=new Panel; f.init; } public java2(String str){ super(str); } public void init{ addWindowListener(new WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } }); setSize(200,200); setLayout(new FlowLayout); lab=new Label("Please press your keyboard!"); add(lab); addKeyListener(this); setVisible(true); } public void keyTyped(KeyEVent e){ lab.setText("\"+ +"\ is pressed!"); repaint; } public void keyPressed(KeyEVent e){ ) public void keyReleased(KeyEvent e){ } private Label lab; }
查看答案
搜索结果不匹配?点我反馈
更多问题