这个程序输入按大小写都可以的写用shift+“a”,小写直接按a
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Print {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
final JLabel jLabel = new JLabel("字母键!");
jFrame.setLayout(null);
jLabel.setBounds(80, 50, 500, 80);
jFrame.add(jLabel);
jFrame.setSize(200, 200);
jFrame.setLocation(200, 300);
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.addKeyListener(new KeyAdapter() {
int n = 0;
char[] a = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
char c = e.getKeyChar();
for (int i = 0; i < 52; i++) {
if (a[i] == c)
jLabel.setText(get5(a[i]));
}
}
public String get5 (char c) {
String s = "";
for (int i = 0; i < 5; i++) {
s += String.valueOf(c);
}
return s;
}
});
}
}
那的用到事件响应机制
actionlistener就能搞定他,其实就是一个键盘相应机制啦..