標題:
鍵盤事件 (一)
[打印本頁]
作者:
tonyh
時間:
2021-9-17 20:16
標題:
鍵盤事件 (一)
一、
鍵盤事件說明
主控制台模式下應用程式主要是使用鍵盤輸入資料,按鍵後,再進行事件處理。因此事件處理很單純只有一種,不必特別宣告、建立…等。但是在視窗應用程式中,當使用者用鍵盤輸入資料時,其按下或鬆開按鍵皆會觸動鍵盤事件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();
}
}
複製代碼
作者:
孫嘉駿
時間:
2021-9-17 20:55
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
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 static void main(String[] args) {
new Ch01();
}
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(arg0.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
if(arg0.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(arg0.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x, y);
}
@Override
public void keyReleased(KeyEvent arg0) {}
@Override
public void keyTyped(KeyEvent arg0) {}
}
複製代碼
作者:
陳宥穎
時間:
2021-9-17 21:01
本帖最後由 陳宥穎 於 2021-9-21 13:04 編輯
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class JPD04 implements KeyListener{
JFrame j1;
JLabel lb1;
ImageIcon ic1,baby;
int x=100,y=100;
JPD04()
{
baby=new ImageIcon(JPD04.class.getResource("pic2/baby.png"));
lb1=new JLabel();
lb1.setBounds(100, 100, 234,351);
lb1.setIcon(baby);
ic1 =new ImageIcon(JPD04.class.getResource("pic2/icon.png"));
j1=new JFrame("按方向鍵移動圖形");
j1.setBounds(100, 100, 500, 400);
j1.setVisible(true);
j1.setLayout(null);
j1.setResizable(false);
j1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
j1.addKeyListener(this);
j1.setIconImage(ic1.getImage());
j1.add(lb1);
}
public static void main(String[] args) {
new JPD04();
}
@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;
}
lb1.setLocation(x,y);
}
@Override
public void keyReleased(KeyEvent e) {
// TODO 自動產生的方法 Stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO 自動產生的方法 Stub
}
}
複製代碼
作者:
黃宥華
時間:
2021-9-23 23:54
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Ch17 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Ch17()
{
icon1=new ImageIcon(Ch17.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch17.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 Ch17();
}
}
複製代碼
作者:
沈子晏
時間:
2021-9-24 18:50
本帖最後由 沈子晏 於 2021-9-24 18:53 編輯
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch0002 implements KeyListener{
JFrame f;
ImageIcon icon1, icon2;
JLabel l;
int x=100, y=100;
public Ch0002() {
icon1=new ImageIcon(Ch0002.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch0002.class.getResource("pic/baby.png"));
l=new JLabel(icon2);
l.setBounds(x, y, 128, 128);
f=new JFrame("按方向鍵移動圖形");
f.setIconImage(icon1.getImage());
f.setBounds(100, 100, 410, 320);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);
f.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;
l.setLocation(x, y);
}
@Override
public void keyReleased(KeyEvent e){}
@Override
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new Ch0002();
}
}
複製代碼
作者:
林政瑜
時間:
2021-9-24 18:52
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
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 static void main(String[] args) {
new Ch01();
}
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(arg0.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
if(arg0.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(arg0.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
lb.setLocation(x, y);
}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
}
複製代碼
作者:
李宇澤
時間:
2021-9-24 19:15
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 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
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_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 Ch90();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2