代码如下
运行结果如下
本文地址:http://liuyanzhao.com/4087.html
转载请注明
- package com.liuyanzhao;
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.Panel;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.WindowConstants;
- public class Demo2 {
- public static void main(String[] args) {
- GUI g = new GUI();
- }
- }
- class GUI extends JFrame {
- JTextArea textArea;
- public GUI() {
- JFrame frame = new JFrame("键盘测试");
- JPanel panel = new JPanel();
- textArea = new JTextArea();
- panel.add(textArea);
- panel.setBounds(100, 100, 100, 100);
- textArea.addKeyListener(new MyListener());
- textArea.append("开始吧:\n");
- frame.add(textArea);
- frame.setSize(400, 400);
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setVisible(true);
- }
- class MyListener implements KeyListener {
- @Override // 按下
- public void keyPressed(KeyEvent e) {
- textArea.append("按下:"+KeyEvent.getKeyText(e.getKeyCode()) + "\n");
- }
- @Override // 松开
- public void keyReleased(KeyEvent e) {
- textArea.append("松开:" + KeyEvent.getKeyText(e.getKeyCode()) + "\n");
- if(KeyEvent.getKeyText(e.getKeyCode()).equals("C")) {
- textArea.setText("");
- }
- }
- @Override // 输入的内容
- public void keyTyped(KeyEvent e) {
- textArea.append("输入:" + e.getKeyChar() + "\n");
- }
- }
- }
运行结果如下
本文地址:http://liuyanzhao.com/4087.html
转载请注明
2018年11月23日 11:42:44
中文输入法下为什么无效