標題:
鍵盤事件
[打印本頁]
作者:
tonyh
時間:
2017-5-26 20:26
標題:
鍵盤事件
本帖最後由 tonyh 於 2019-4-27 13:48 編輯
一、
鍵盤事件說明
主控制台模式下應用程式主要是使用鍵盤輸入資料,按鍵後,再進行事件處理。因此事件處理很單純只有一種,不必特別宣告、建立…等。但是在視窗應用程式中,當使用者用鍵盤輸入資料時,其按下或鬆開按鍵皆會觸動鍵盤事件KeyEvent。
Java在鍵盤的傾聽機制,有兩種方式來處理觸發的事件,分別是「實作KeyListener介面」與「繼承KeyAdapter類別」,在本單元我們將練習實作KeyListener介面。
二、
KeyListener介面與KeyAdapter類別皆提供了三種鍵盤處理事件方法
1. void keyPressed(KeyEvent e)
當鍵盤按鍵被按下時所觸發的事件。
2. void keyReleased(KeyEvent e)
當鍵盤已按下的按鍵被放開時所觸發的事件。
3. void keyTyped(KeyEvent e)
鍵入字元事件,即按下與放開的整個過程。此時,會產生按鍵的對應字元,但有些按鍵如Ctrl或Shift等沒有字元對應。
三、
KeyEvent類別常用的方法
1. int getkeyCode()
當按下或放開了某一個按鍵,會傳回一個整數型態的鍵盤碼。而鍵盤事件類別KeyEvent為一些常用的按鍵定義了常數代碼,如下:
① 0 ~ 9數字:KeyEvent.VK_0 ~ KeyEvent.VK_9。
② A ~ Z字母:KeyEvent.VK_A ~ KeyEvent.VK_Z。
③ 方向鍵:KeyEvent.VK_UP、KeyEvent.VK_DOWN、KeyEvent.VK_LEFT、KeyEvent.VK_RIGHT。
④ 常用鍵:KeyEvent.VK_SHIFT、KeyEvent.VK_CONTROL、KeyEvent.VK_ALT、KeyEvent.VK_ENTER…等。
2. char getkeyChar()
當按下或放開了某一個按鍵,傳回該按鍵的字元。
3. boolean isShiftDown()
當Shift鍵被按時,傳回值true。
4. boolean isControlDown()
當Ctrl鍵被按時,傳回值true。
5. boolean isAltDown()
當Alt鍵被按時,傳回值true。
四、
實作KeyListener介面
鍵盤事件傾聽者介面為「KeyListener」,鍵盤事件「事件來源」就是視窗物件,要將視窗物件與鍵盤事件傾聽者連結在一起(註冊傾聽者),要使用 addKeyListener(this) 敘述。
素材:
baby.png
icon.png
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Ch93 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Ch93()
{
icon1=new ImageIcon(Ch93.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch93.class.getResource("pic/baby.png"));
lb=new JLabel(icon2);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("按方向建移動圖形");
fm.setIconImage(icon1.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.addKeyListener(this);
fm.add(lb);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x, y);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new Ch93();
}
}
複製代碼
作者:
李知易
時間:
2017-5-26 21:34
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Ch91 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Ch91()
{
icon1=new ImageIcon(Ch91.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch91.class.getResource("pic/baby.png"));
lb=new JLabel(icon2);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("按方向建移動圖形");
fm.setIconImage(icon1.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.addKeyListener(this);
fm.add(lb);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x, y);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new Ch91();
}
}
複製代碼
作者:
洪振庭
時間:
2017-5-27 10:27
本帖最後由 洪振庭 於 2017-6-4 13:36 編輯
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Chx2 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Chx2()
{
icon1=new ImageIcon(Chx2.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Chx2.class.getResource("pic/奧尼克西亞.png"));
lb=new JLabel(icon2);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("按方向建移動奧妮克西亞");
fm.setIconImage(icon1.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.addKeyListener(this);
fm.add(lb);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x, y);
}
public static void main(String[] args) {
new Chx2();
}
}
複製代碼
作者:
梁和雋
時間:
2017-6-2 13:21
本帖最後由 梁和雋 於 2017-6-2 20:01 編輯
import javax.swing.*;
import java.awt.event.*;
public class Ch01 implements KeyListener
{
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Ch01()
{
icon1=new ImageIcon(Ch01.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch01.class.getResource("pic/baby.png"));
lb=new JLabel(icon2);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("按方向鑑移動圖形");
fm.setIconImage(icon1.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.addKeyListener(this);
fm.add(lb);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x, y);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args)
{
new Ch01();
}
}
複製代碼
作者:
曾挺桂
時間:
2017-6-2 19:58
本帖最後由 曾挺桂 於 2017-6-2 20:00 編輯
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Ch93 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Ch93()
{
icon1=new ImageIcon(Ch93.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch93.class.getResource("pic/baby.png"));
lb=new JLabel(icon2);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("按方向鍵移動圖形");
fm.setIconImage(icon1.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.addKeyListener(this);
fm.add(lb);
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x, y);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new Ch93();
}
}
複製代碼
作者:
黃璽安
時間:
2017-6-2 20:19
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class CH90 {
private JFrame fm;
private JLabel lb;
private ImageIcon icon1,icon2;
private int x=100,y=100;
CH90()
{
icon1=new ImageIcon(CH90.class.getResource("pic/icon.png"));
icon2=new ImageIcon(CH90.class.getResource("pic/baby.png"));
lb=new JLabel(icon2);
lb.setBounds(x,y,128,128);
fm=new JFrame("按方向鍵移動圖形");
fm.setIconImage(icon1.getImage());
fm.setBounds(100,100,410,320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.addKeyListener(this);
fm.add(lb);
}
public void KeyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP);
y+=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN);
y-=5;
if(e.getKeyCode()==KeyEvent.VK_RIGHT);
x+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT);
x-=5;
lb.setLocation(x, y);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new CH90();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2