標題:
鍵盤事件 (二)
[打印本頁]
作者:
tonyh
時間:
2017-6-2 20:04
標題:
鍵盤事件 (二)
本帖最後由 tonyh 於 2019-4-27 13:48 編輯
承上個主題,判斷 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();
}
}
複製代碼
作者:
李知易
時間:
2017-6-2 20:44
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Ch92 extends KeyAdapter
{
JFrame fm;
JLabel lb;
ImageIcon ii,ic;
int x=200, y=100;
Ch92()
{
ic=new ImageIcon(Ch92.class.getResource("pic/icon.png"));
ii=new ImageIcon(Ch92.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 Ch92();
}
}
複製代碼
作者:
洪振庭
時間:
2017-6-2 20: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 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/奧尼克西亞.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;
}
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();
}
}
複製代碼
作者:
曾挺桂
時間:
2017-6-2 20:58
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();
}
}
複製代碼
作者:
梁和雋
時間:
2017-6-2 21:01
[code]import javax.swing.*;
import java.awt.event.*;
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();
}
}
複製代碼
[/code]
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2