標題:
鍵盤事件 (一)
[打印本頁]
作者:
tonyh
時間:
2020-1-6 20:39
標題:
鍵盤事件 (一)
一、
鍵盤事件說明
主控制台模式下應用程式主要是使用鍵盤輸入資料,按鍵後,再進行事件處理。因此事件處理很單純只有一種,不必特別宣告、建立…等。但是在視窗應用程式中,當使用者用鍵盤輸入資料時,其按下或鬆開按鍵皆會觸動鍵盤事件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();
}
}
複製代碼
作者:
洪翊庭
時間:
2020-1-6 21:03
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Ch87 implements KeyListener {
private JFrame fm;
private ImageIcon icon1,icon2;
private JLabel lb;
private int x=100,y=100;
Ch87()
{
icon1=new ImageIcon(Ch87.class.getResource("pic/icon.png"));
icon2=new ImageIcon(Ch87.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 Ch87();
}
}
複製代碼
作者:
洪翊展
時間:
2020-1-6 21:03
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 void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=100;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=100;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=100;
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=100;
lb.setLocation(x, y);
}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new Ch01();
}
}
複製代碼
作者:
黃宇綸
時間:
2020-1-11 14:43
本帖最後由 黃宇綸 於 2020-1-11 15:28 編輯
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 {
JFrame fm;
ImageIcon ii,ic;
JLabel lb;
int x=100,y=100;
Ch01()
{
ii=new ImageIcon(Ch01.class.getResource("pic/icon.png"));
ic=new ImageIcon(Ch01.class.getResource("pic/baby.png"));
lb=new JLabel(ic);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("按方向建移動圖形");
fm.setIconImage(ii.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();
}
}
複製代碼
作者:
李沛昂
時間:
2020-1-12 15:46
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Ch00 implements KeyListener{
private JFrame fm;
private JLabel lb;
private ImageIcon icon1,icon2;
private int x=100,y=100;
Ch00()
{
icon1=new ImageIcon(Ch00.class.getResource("icon/pic.png"));
icon2=new ImageIcon(Ch00.class.getResource("baby/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 static void main(String[] args) {
new Ch00();
}
@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) {}
}
複製代碼
作者:
黃宇瑄
時間:
2020-1-12 22:43
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 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();
}
}
複製代碼
作者:
黃宇綸
時間:
2020-1-20 20:17
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Ch01 extends KeyAdapter{
JFrame fm;
JLabel lb;
ImageIcon ii,ic;
int x=200, y=100;
Ch01()
{
ic=new ImageIcon(Ch01.class.getResource("pic/icon.png"));
ii=new ImageIcon(Ch01.class.getResource("pic/baby.png"));
lb=new JLabel(ii);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("鍵盤事件");
fm.setIconImage(ic.getImage());
fm.setBounds(100, 100, 500, 350);
fm.setVisible(true);
fm.setResizable(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
fm.addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
if(!e.isShiftDown())
{
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;
}else
{
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 Ch01();
}
}
複製代碼
作者:
鄭楀諺
時間:
2020-2-8 13:31
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class ch93 implements KeyListener{
JFrame fm;
JLabel lb;
ImageIcon baby, ic;
int x=100, y=100;
ch93()
{
baby = new ImageIcon(ch93.class.getResource("pic/baby.png"));
ic = new ImageIcon(ch93.class.getResource("pic/icon.png"));
lb = new JLabel(baby);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("按方向建移動圖形");
fm.setIconImage(ic.getImage());
fm.setBounds(100, 100, 300, 250);
fm.setResizable(false);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.add(lb);
fm.addKeyListener(this);
}
public static void main(String[] args) {
// TODO 自動產生的方法 Stub
new ch93();
}
@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 arg0) {}
@Override
public void keyTyped(KeyEvent arg0) {}
}
複製代碼
作者:
趙一鳴
時間:
2020-2-28 12:28
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch93 implements KeyListener
{
JFrame fm;
ImageIcon ic,bk;
JLabel lb_ic;
int x=100,y=100;
Ch93()
{
ic=new ImageIcon(Ch93.class.getResource("pic/baby.png"));
bk=new ImageIcon(Ch93.class.getResource("pic/icon.png"));
lb_ic=new JLabel(ic);
lb_ic.setBounds(x, y,128, 128);
fm=new JFrame("按方向移動圖形");
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setBounds(100, 100, 410, 320);
fm.setIconImage(bk.getImage());
fm.addKeyListener(this);
fm.add(lb_ic);
}
public static void main(String[] args)
{
new Ch93();
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
lb_ic.setLocation(x,y);
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}
}
複製代碼
作者:
趙一鳴
時間:
2020-2-28 12:29
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch93 implements KeyListener
{
JFrame fm;
ImageIcon ic,bk;
JLabel lb_ic;
int x=100,y=100;
Ch93()
{
ic=new ImageIcon(Ch93.class.getResource("pic/baby.png"));
bk=new ImageIcon(Ch93.class.getResource("pic/icon.png"));
lb_ic=new JLabel(ic);
lb_ic.setBounds(x, y,128, 128);
fm=new JFrame("按方向移動圖形");
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setBounds(100, 100, 410, 320);
fm.setIconImage(bk.getImage());
fm.addKeyListener(this);
fm.add(lb_ic);
}
public static void main(String[] args)
{
new Ch93();
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
x+=5;
if(e.getKeyCode()==KeyEvent.VK_LEFT)
x-=5;
if(e.getKeyCode()==KeyEvent.VK_UP)
y-=5;
if(e.getKeyCode()==KeyEvent.VK_DOWN)
y+=5;
lb_ic.setLocation(x,y);
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2