Swing KeyLintener keyboard event listener

package Assis;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class TestEvent extends JFrame {
        private static final long serialVersionUID = 1L;

        public TestEvent() {
                setSize(400, 300);
                // addKeyListener(new Listener());
                P p1 = new P();
                System.out.println(p1.isFocusable());
                p1.setFocusable(true);
                p1.setBackground(Color.red);
                this.getContentPane().add(BorderLayout.NORTH, p1);

                P p2 = new P();
                p2.setBackground(Color.green);
                this.getContentPane().add(BorderLayout.SOUTH, p2);

                setVisible(true);
                this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }

        public static void main(String[] args) {
                new TestEvent();
        }
}

class P extends JPanel {
        private static final long serialVersionUID = 1L;

        public P() {
                addKeyListener(new Listener());

                this.addMouseListener(new MouseAdapter() {
                        public void mouseClicked(MouseEvent e) {
                                System.out.println(e);
                        }
                });
        }
}

class Listener implements KeyListener {
        public void keyPressed(KeyEvent e) {
                System.out.println("---" + e);
        }

        public void keyReleased(KeyEvent e) {

        }

        public void keyTyped(KeyEvent e) {

        }
}
分类:Java 时间:2010-03-29 人气:422
分享到:
blog comments powered by Disqus

相关文章

  • public static void main (String args []) parameters 2010-05-15

    String args [] is the main function of main parameters, namely array of strings. Using eclipse in the editing, right-click to run the Java files in the Run As option, select Run Configurations. Main class in the Project and complete the project, resp

  • Why public static void main (String args []){} 2010-11-06

    main () is a Java program entry, program execution is the beginning of this entry. Class is a class member, main () method must be public members. So that it can be called in the execution environment. main () method does not produce the object can b

  • 浅析C#中的Main(String[] args)参数输入问题 2014-07-20

    本篇文章主要是对C#中的Main(String[] args)参数输入问题进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 指定相关的测试代码 首先,写一个用于测试的关于Main(String[] args)参数输入有关的代码类,如下: using System; public class Hello { public static void Main(String[] args) { switch (args[0]) { case "老板": Console.Write

  • (Transfer) JAVA face questions FAQ Series (6) - string (String) topics for 2010-04-22

    Once we have reviewed the side with the exam questions in the end often several String objects to create knowledge, this time we are entitled to interview several common primer, to a review of String objects related to some other areas. 1, String cla

  • JAVA face questions FAQ Series (6) - string (String) topics for collection 2010-05-27

    JAVA face questions FAQ Series (6) - string (String) topics for collection JAVA face questions FAQ Series (6) - string (String) topics for The last time we have together reviewed the exam in the end often face questions a few String objects to create

  • Java, how to determine the empty string 2010-03-29

    The following is a Java to determine whether the empty string in several ways. Method One: Most people use a method, intuitive and convenient, but inefficient. Method 2: To compare the length of the string, high efficiency, yes I know the best method

  • java how to judge a Chinese string 2010-11-16

    There is a Character.UnicodeBlock in Java static class, Character.UnicodeBlock.of (char c) Returns the character literal. For example: class Test { / / Determine whether the Chinese character private boolean isChinese (char c) { Character.UnicodeBloc

  • java access modifiers public protected friendly private usage summary 2009-04-01

    First, the statement: java in, friendly, and this modifier is not an explicit statement of the member variables and methods of the former would not have any modifiers, the default is friendly. For clarity, sub-summed up in three different circumstanc

  • Switch: java database manipulation of the main lob types of data 2010-04-11

    Blob Clob and is divided into three kinds of operations: insert, update, and reading. For insertion, can be divided into two categories. One is the normal field can handle, one must first insert a blank for the clob / blob and then update the actual

  • Switch: java database manipulation lob the main types of data 2010-10-24

    Articles Category: Java Programming Blob Clob and divided into three kinds of operations: insert, update and read the display. For the insert, can be divided into two categories. One is the normal field can be directly handled by a class must first i

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.470 (s). 12 q(s)