標題:
鍵盤事件 (二)
[打印本頁]
作者:
tonyh
時間:
2020-1-20 19:34
標題:
鍵盤事件 (二)
承上個主題,判斷 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();
}
}
複製代碼
作者:
洪翊展
時間:
2020-1-20 20: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 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.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 static void main(String[] args) {
new Ch01();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO 自動產生的方法 Stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO 自動產生的方法 Stub
}
}
複製代碼
作者:
黃宇瑄
時間:
2020-1-20 20:18
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();
}
}
複製代碼
作者:
洪翊庭
時間:
2020-1-20 20:26
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();
}
}
複製代碼
作者:
趙一鳴
時間:
2020-1-20 20:38
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch90 implements KeyListener
{
JFrame fm;
JLabel lb;
ImageIcon ii,ic;
int x=200;
int y=200;
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.add(lb);
fm.addKeyListener(this);
fm.setTitle("鍵盤事件");
fm.setBounds(100, 100, 500, 350);
fm.setResizable(false);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.setLayout(null);
fm.setIconImage(ic.getImage());
}
public static void main(String[] args)
{
new Ch90();
}
@Override
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);
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}
複製代碼
作者:
趙一鳴
時間:
2020-1-20 21:08
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch91 implements KeyListener{
JFrame fm;
JLabel lb;
int x=100,y=100;
ImageIcon ic,pic1,pic2,pic3,pic4;
Ch91()
{
ic=new ImageIcon(Ch91.class.getResource("pic/android.png"));
pic1=new ImageIcon(Ch91.class.getResource("pic/1.png"));
pic2=new ImageIcon(Ch91.class.getResource("pic/2.png"));
pic3=new ImageIcon(Ch91.class.getResource("pic/3.png"));
pic4=new ImageIcon(Ch91.class.getResource("pic/4.png"));
lb=new JLabel(pic2);
lb.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, 500, 500);
fm.setIconImage(ic.getImage());
fm.add(lb);
fm.addKeyListener(this);
}
public static void main(String[] args)
{
new Ch91();
}
@Override
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_UP)
{
y-=5;
lb.setIcon(pic1);
lb.setLocation(x, y);
}
if(e.getKeyCode()==KeyEvent.VK_DOWN)
{
y+=5;
lb.setIcon(pic4);
lb.setLocation(x, y);
}
if(e.getKeyCode()==KeyEvent.VK_LEFT)
{
x-=5;
lb.setIcon(pic3);
lb.setLocation(x, y);
}
if(e.getKeyCode()==KeyEvent.VK_RIGHT)
{
x+=5;
lb.setIcon(pic2);
lb.setLocation(x, y);
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}
複製代碼
作者:
戴嘉禾
時間:
2020-2-17 16:11
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();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2