標題:
鍵盤事件 (二)
[打印本頁]
作者:
tonyh
時間:
2021-9-24 19:04
標題:
鍵盤事件 (二)
承上個主題,判斷 Shift 鍵是否被按住,使執行時若搭配 Shift 鍵,則上下左右鍵所造成的方向會顛倒。
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Ch90 extends KeyAdapter{
JFrame fm;
JLabel lb;
ImageIcon ii,ic;
int x=200, y=100;
Ch90()
{
ic=new ImageIcon(Ch90.class.getResource("pic/icon.png"));
ii=new ImageIcon(Ch90.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()==false)
{
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 Ch90();
}
}
複製代碼
作者:
董宸佑
時間:
2021-9-24 19:16
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()==false)
{
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();
}
}
複製代碼
作者:
林政瑜
時間:
2021-9-24 19:16
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.isShiftDown()==true){
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);
}
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 void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new Ch90();
}
}
複製代碼
作者:
黃宥華
時間:
2021-9-24 19:18
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.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 void keyReleased(KeyEvent e){
}
public void keyTyped(KeyEvent e){
}
public static void main(String[] args) {
new Ch17();
}
}
複製代碼
作者:
陳宥穎
時間:
2021-9-24 19:25
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.isShiftDown()==false)
{
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);
}
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 void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
public static void main(String[] args) {
new Ch90();
}
}
複製代碼
作者:
孫嘉駿
時間:
2021-9-24 19:27
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
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.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
if(e.isShiftDown()==false)
{
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();
}
}
複製代碼
作者:
蔡忻霓
時間:
2021-9-24 19:31
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class JPA01 extends KeyAdapter{
JFrame fm;
JLabel lb;
ImageIcon ii,ic;
int x=200, y=100;
JPA01()
{
ic=new ImageIcon(JPA01.class.getResource("pic/icon.png"));
ii=new ImageIcon(JPA01.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()==false)
{
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 JPA01();
}
}
複製代碼
作者:
沈子晏
時間:
2021-9-24 19:59
import java.awt.event.KeyAdapter;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.event.KeyEvent;
public class Ch001 extends KeyAdapter {
JFrame fm;
JLabel lb;
ImageIcon icon1,icon2;
int x=200, y=100;
Ch001(){
icon2=new ImageIcon(Ch001.class.getResource("pic/icon.png"));
icon1=new ImageIcon(Ch001.class.getResource("pic/baby.png"));
lb=new JLabel(icon1);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("鍵盤事件");
fm.setIconImage(icon2.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()==false)
{
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 Ch001();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2