標題:
鍵盤事件 (二)
[打印本頁]
作者:
tonyh
時間:
2019-4-27 15: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();
}
}
複製代碼
作者:
蔡幸融
時間:
2019-4-27 15:18
本帖最後由 蔡幸融 於 2019-5-11 14:20 編輯
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch87 implements KeyListener{
JFrame fm;
JLabel lb;
ImageIcon ic, baby;
int x=100, y=100;
Ch87()
{
ic=new ImageIcon(Ch87.class.getResource("pic/icon.png"));
baby=new ImageIcon(Ch87.class.getResource("pic/baby.png"));
lb=new JLabel(baby);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("靈異事件");
fm.setIconImage(ic.getImage());
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addKeyListener(this);
}
@Override
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;
}
}
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);
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new Ch87();
}
}
複製代碼
作者:
蕭澧邦
時間:
2019-5-11 14:25
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();
}
}
複製代碼
作者:
高睿辰
時間:
2019-5-11 14:25
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();
}
}
複製代碼
作者:
黃宥鈞
時間:
2019-5-11 14: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();
}
}
複製代碼
作者:
莊旻叡
時間:
2019-5-11 14:29
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();
}
}
複製代碼
作者:
譚暐霖
時間:
2019-5-22 16:16
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Ch99 implements KeyListener{
JFrame fm;
JLabel lb;
ImageIcon baby;
int x=100, y=100;
Ch99()
{
baby=new ImageIcon(Ch99.class.getResource("pic/baby.jpg"));
lb=new JLabel(baby);
lb.setBounds(x, y, 128, 128);
fm=new JFrame("Weird Baby");
fm.setBounds(100, 100, 410, 320);
fm.setVisible(true);
fm.setResizable(false);
fm.setLayout(null);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fm.add(lb);
fm.addKeyListener(this);
}
@Override
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;
}
}
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);
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
public static void main(String[] args) {
new Ch99();
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/)
Powered by Discuz! 7.2