標題:
鍵盤事件
[打印本頁]
作者:
tonyh
時間:
2019-4-27 13:53
標題:
鍵盤事件
一、
鍵盤事件說明
主控制台模式下應用程式主要是使用鍵盤輸入資料,按鍵後,再進行事件處理。因此事件處理很單純只有一種,不必特別宣告、建立…等。但是在視窗應用程式中,當使用者用鍵盤輸入資料時,其按下或鬆開按鍵皆會觸動鍵盤事件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();
}
}
複製代碼
作者:
黃宥鈞
時間:
2019-4-27 14:53
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();
}
}
複製代碼
作者:
蔡幸融
時間:
2019-4-27 14:57
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch87 implements KeyListener{
JFrame fm;
JLabel lb;
ImageIcon ic, baby;
int x=100, y=100;
Ch87()
{
ic=new ImageIcon(Ch87.class.getResource("pic/icon.png"));
baby=new ImageIcon(Ch87.class.getResource("pic/baby.png"));
lb=new JLabel(baby);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("靈異事件");
fm.setIconImage(ic.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addKeyListener(this);
}
@Override
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);
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new Ch87();
}
}
複製代碼
作者:
蕭澧邦
時間:
2019-4-27 14:57
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();
}
}
複製代碼
作者:
莊旻叡
時間:
2019-5-11 14: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 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();
}
}
複製代碼
作者:
高睿辰
時間:
2019-5-11 14: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 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();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2