標題:
鍵盤事件
[打印本頁]
作者:
tonyh
時間:
2016-4-30 16:08
標題:
鍵盤事件
本帖最後由 tonyh 於 2017-5-26 20:26 編輯
一、
鍵盤事件說明
主控制台模式下應用程式主要是使用鍵盤輸入資料,按鍵後,再進行事件處理。因此事件處理很單純只有一種,不必特別宣告、建立…等。但是在視窗應用程式中,當使用者用鍵盤輸入資料時,其按下或鬆開按鍵皆會觸動鍵盤事件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) 敘述。
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();
}
}
複製代碼
作者:
張峻瑋
時間:
2016-4-30 17:23
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=new JFrame("按方向鍵移動圖形");
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(0, 0, 128, 128);
fm.setIconImage(icon1.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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();
}
}
複製代碼
作者:
劉得恩
時間:
2016-4-30 17:24
import javax.swing.*;
import java.awt.event.*;
public class Ch93 implements KeyListener{
private JFrame f;
private JLabel lb;
private ImageIcon icon1,icon2;
private int x=200,y=200;
Ch93()
{
f=new JFrame("\ucfcf\uff9a");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.setLayout(null);
f.setResizable(false);
f.setBounds(100,100,500,500);
f.addKeyListener(this);
icon1=new ImageIcon(Ch93.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch93.class.getResource("pic/baby.png"));
f.setIconImage(icon1.getImage());
lb=new JLabel(icon2);
lb.setBounds(x,y,128,128);
f.add(lb);
}
public static void main(String[] args)
{
new Ch93();
}
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
else if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
else if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
else if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x,y);
}
public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e){}
}
複製代碼
作者:
李允軒
時間:
2016-4-30 17:27
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 JLabel lb;
private ImageIcon icon1,icon2;
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("123456789");
fm.setIconImage(icon1.getImage());
fm.setVisible(true);
fm.setBounds(100,100,410,320);
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) {
Ch93 app=new Ch93();
}
}
複製代碼
作者:
林宇翔
時間:
2016-4-30 17:32
package ch93;
import javax.swing.JFrame;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.ActionEvent;
public class Ch93 implements KeyListener{
private JFrame fm=new JFrame("按方向鍵移動圖形");
private JLabel lb;
private ImageIcon icon1,icon2;
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(0, 0, 128,128);
fm.setIconImage(icon1.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.addKeyListener(this);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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();
}
}
複製代碼
作者:
張彥承
時間:
2016-6-16 19:26
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class A82 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Ch93()
{
icon1=new ImageIcon(A82.class.getResource("pic/icon.png"));
icon2=new ImageIcon(A82.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 A82();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2