VietTuts

Tự Học Lập Trình Online

  • Home
  • Java
  • Servlet
  • JSP
  • Struts2
  • Hibernate
  • Spring
  • MyBatis
  • Java WS
  • C
  • C++
  • Python
  • PHP
  • Eclipse
  • VBA
  • Web
    • JavaScript
    • JQUERY
    • JSON
    • AJAX
    • CSS
    • HTML
    • Node.js
    • Angular 7
  • SQL
    • MySQL
    • SQL Server
  • Misc
    • Phần mềm tiện ích
    • Cấu trúc dữ liệu và giải thuật
    • Học lập trình C#
    • Selenium Test
Java Cơ Bản Các Khái Niệm Java OOPs Java String Xử Lý Ngoại Lệ Các Lớp Lồng Nhau

Java AWT

AWT trong java Xử lý sự kiện Java AWT Button trong Java AWT Label trong Java AWT TextField trong Java AWT TextArea trong Java AWT CheckBox trong Java AWT CheckBoxGroup trong Java AWT Choice trong Java AWT List trong Java AWT Canvas trong Java AWT Scrollbar trong Java AWT MenuItem và Menu trong Java AWT PopupMenu trong Java AWT Panel trong Java AWT Dialog trong Java AWT Toolkit trong Java AWT ActionListener trong Java AWT MouseListener trong Java AWT MouseMotionListener trong Java AWT ItemListener trong Java AWT KeyListener trong Java AWT WindowListener trong Java AWT Close Window trong Java AWT Các lớp Adapter trong Java AWT
Java Swing Java I/O Ví Dụ Java I/O Lập Trình Mạng Với Java Java Date Chuyển Đối Kiểu Dữ Liệu Java Collections Java JDBC Các Tính Năng Mới Trong Java Bài Tập Java Có Lời Giải Câu Hỏi Phỏng Vấn Java

KeyListener trong Java AWT


ItemListener trong Java AWT
WindowListener trong Java AWT

Java KeyListener được gọi bất cứ khi nào bạn thay đổi trạng thái của bàn phím (key). Interface KeyListener thuộc về gói java.awt.event. Nó có ba phương thức.

Các phương thức của interface KeyListener

Interface KeyListener có 3 phương thức được đưa ra như dưới đây.

public abstract void keyPressed(KeyEvent e);  
public abstract void keyReleased(KeyEvent e);  
public abstract void keyTyped(KeyEvent e);  

Nội dung chính

  • Ví dụ về Java KeyListener trong Java AWT
  • Ví dụ 2 về Java KeyListener trong Java AWT - Đếm số từ và ký tự

Ví dụ về Java KeyListener trong Java AWT

package vn.viettuts.awt;

import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyListenerExample1 extends Frame implements KeyListener {
    private Label label;
    private TextArea textArea;

    public KeyListenerExample1() {
        label = new Label();
        label.setBounds(20, 50, 100, 20);
        textArea = new TextArea();
        textArea.setBounds(20, 80, 300, 300);
        textArea.addKeyListener(this);

        add(label);
        add(textArea);
        setSize(400, 400);
        setLayout(null);
        setVisible(true);
    }

    public void keyPressed(KeyEvent e) {
        label.setText("Key Pressed");
    }

    public void keyReleased(KeyEvent e) {
        label.setText("Key Released");
    }

    public void keyTyped(KeyEvent e) {
        label.setText("Key Typed");
    }

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

Kết quả:

Ví dụ KeyListener trong Java AWT

Ví dụ 2 về Java KeyListener trong Java AWT - Đếm số từ và ký tự

package vn.viettuts.awt;

import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class KeyListenerExample2 extends Frame implements KeyListener {
    private Label label;
    private TextArea textArea;

    public KeyListenerExample2() {
        label = new Label();
        label.setBounds(20, 50, 200, 20);
        textArea = new TextArea();
        textArea.setBounds(20, 80, 300, 300);
        textArea.addKeyListener(this);

        add(label);
        add(textArea);
        setSize(400, 400);
        setLayout(null);
        setVisible(true);
    }

    public void keyPressed(KeyEvent e) {
    }

    public void keyReleased(KeyEvent e) {
        String text = textArea.getText();
        String words[] = text.split("\\s");
        label.setText("Words: " + words.length + " Characters:" + text.length());
    }

    public void keyTyped(KeyEvent e) {
    }

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

Kết quả:

Ví dụ KeyListener trong Java AWT
ItemListener trong Java AWT
WindowListener trong Java AWT

Recent Updates

Toán tử dấu 2 chấm (::) trong Java 8Lambda Expression - Biểu thức Lambda trong java 8Bài tập Java - Sắp xếp nhanh (Quick Sort) trong JavaBài tập Java - Sắp xếp chèn (Insertion Sort) trong JavaBài tập Java - Sắp xếp nổi bọt (Bubble Sort) trong JavaBài tập quản lý sinh viên trong JavaHashSet trong javaHashMap trong javaLớp WindowAdapter trong Java SwingLớp MouseMotionAdapter trong Java SwingLớp MouseAdapter trong Java SwingLớp KeyAdapter trong Java Swing

VietTuts on facebook

Học Lập Trình Online Miễn Phí - VietTuts.Vn

Danh sách bài học

Học java
Học servlet
Học jsp
Học Hibernate
Học Struts2
Học Spring
Học SQL

Câu hỏi phỏng vấn

201 câu hỏi phỏng vấn java
25 câu hỏi phỏng vấn servlet
75 câu hỏi phỏng vấn jsp
52 câu hỏi phỏng vấn Hibernate
70 câu hỏi phỏng vấn Spring
57 câu hỏi phỏng vấn SQL

About VietTuts.Vn

Hệ thống bài học trên VietTuts.Vn bao gồm các bài lý thuyết và thực hành về các công nghệ java và công nghệ web. Các bài lý thuyết trên hệ thống VietTuts.Vn được tham khảo và tổng hợp từ các trang http://javatpoint.com, http://www.tutorialspoint.com, http://docs.oracle.com/en …

Scroll back to top

Copyright © 2016 VietTuts.Vn all rights reserved. | VietTuts.Vn team | Liên hệ | Chính sách - riêng tư | sitemap.html | sitemap_index.xml