標題:
鍵盤事件 (四)
[打印本頁]
作者:
周政輝
時間:
2018-2-24 11:23
標題:
鍵盤事件 (四)
本帖最後由 周政輝 於 2018-2-24 11:24 編輯
設計如下圖所示視窗, 按住鍵盤的方向鍵, 可控制寄居蟹往上下左右四個方向移動, 移動時頭要朝前方.
[attach]3405[/attach]
[attach]3406[/attach]
素材: [attach]3407[/attach]
作者:
蔡庭豪
時間:
2018-2-24 11:45
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
public class movecharetar extends JFrame {
private JPanel contentPane;
private static ImageIcon icon;
private int x =97,y=10;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
movecharetar frame = new movecharetar();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public movecharetar() {
icon = new ImageIcon(udicon.class.getResource("Actions-go-down-icon.png"));
Image img = icon.getImage().getScaledInstance(75,102,java.awt.Image.SCALE_FAST );
ImageIcon newicon = new ImageIcon(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel(newicon);
label.setBounds(97, 10, 100, 100);
contentPane.add(label);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()==e.VK_LEFT){
x-=3;
label.setIcon(turndir("Actions-go-previous-icon.png"));
}
if(e.getKeyCode()==e.VK_UP){
y-=3;
label.setIcon(turndir("Actions-go-up-icon.png"));
}
if(e.getKeyCode()==e.VK_RIGHT){
x+=3;
label.setIcon(turndir("Actions-go-next-icon.png"));
}
if(e.getKeyCode()==e.VK_DOWN){
y+=3;
label.setIcon(turndir("Actions-go-down-icon.png"));
}
label.setLocation(x, y);
}
});
}
public static ImageIcon turndir(String picname){
icon = new ImageIcon(udicon.class.getResource(picname));
Image img = icon.getImage().getScaledInstance(100,100,java.awt.Image.SCALE_FAST );
ImageIcon diricon = new ImageIcon(img);
return diricon;
}
}
複製代碼
作者:
黃茂勛
時間:
2018-2-24 11:57
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main extends JFrame {
private JPanel contentPane;
private int x=100, y=100;
private ImageIcon icon_1 = new ImageIcon(Main.class.getResource("crab.png"));
private ImageIcon icon_2 = new ImageIcon(Main.class.getResource("crab2.png"));
private ImageIcon icon_3 = new ImageIcon(Main.class.getResource("crab3.png"));
private ImageIcon icon_4 = new ImageIcon(Main.class.getResource("crab4.png"));
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
ImageIcon icon = new ImageIcon(Main.class.getResource("icon.png"));
Image img = icon.getImage().getScaledInstance(45, 45, java.awt.Image.SCALE_FAST);
frame.setIconImage(img);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 483, 359);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
ImageIcon icon = new ImageIcon(Main.class.getResource("crab2.png"));
Image img = icon.getImage().getScaledInstance(150, 150, java.awt.Image.SCALE_FAST);
ImageIcon temp = new ImageIcon(img);
JLabel lblNewLabel = new JLabel(temp);
lblNewLabel.setBounds(94, 86, 169, 142);
contentPane.add(lblNewLabel);
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
y-=5;
lblNewLabel.setIcon(icon_1);
}
if(e.getKeyCode() == KeyEvent.VK_DOWN){
y+=5;
lblNewLabel.setIcon(icon_3);
}
if(e.getKeyCode() == KeyEvent.VK_LEFT){
x-=5;
lblNewLabel.setIcon(icon_4);
}
if(e.getKeyCode() == KeyEvent.VK_RIGHT){
x+=5;
lblNewLabel.setIcon(icon_2);
}
lblNewLabel.setLocation(x, y);
}
});
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-2-26 21:38
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class CRAB extends JFrame {
private JPanel contentPane;
private int x=150;
private int y=150;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CRAB frame = new CRAB();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public CRAB() {
setTitle("\u6309\u65B9\u5411\u9375\u79FB\u52D5\u5716\u5F62");
ImageIcon icon = new ImageIcon(CRAB.class.getResource("icon.png"));
Image img = icon.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
setIconImage(img);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 400, 400);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
JLabel label = new JLabel(babyicon);
label.setBounds(150, 150, 100, 100);
contentPane.add(label);
addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent arg0)
{
if(arg0.isShiftDown())
{
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
y+=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-2.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y-=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x+=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-1.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x-=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-3.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
}
else
{
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
y-=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y+=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-2.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-3.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x+=5;
ImageIcon imgbaby = new ImageIcon(CRAB.class.getResource("crab-1.png"));
Image baby = imgbaby.getImage().getScaledInstance(100,100,Image.SCALE_FAST);
ImageIcon babyicon = new ImageIcon(baby);
label.setIcon(babyicon);
}
}
label.setBounds(x,y,100,100);
}
});
}
}
複製代碼
作者:
張健勳
時間:
2018-3-2 23:25
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
private int x=100,y=100;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main() {
icon = new ImageIcon(Main.class.getResource("mochi.png"));
Image img = icon.getImage().getScaledInstance(399, 300, java.awt.Image.SCALE_FAST );
ImageIcon newIcon = new ImageIcon(img);
setIconImage(Toolkit.getDefaultToolkit().getImage(Main.class.getResource("icon.png")));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 864, 645);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel(newIcon);addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if(e.isShiftDown())
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
y+=15;
label.setIcon("MochiR.png");
}if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
y-=15;
label.setIcon("MochiR.png");
}if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
x+=15;
label.setIcon("MochiR.png");
}if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
x-=15;
label.setIcon("MochiR.png");
}
}else
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
y-=15;
label.setIcon("MochiR.png");
}if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
y+=15;
label.setIcon("MochiR.png");
}if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=15;
label.setIcon("MochiR.png");
}if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
label.setIcon("MochiR.png");
x+=15;
}
}
label.setLocation(x,y);
}
});
label.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
}
});
label.setBounds(79, 84, 338, 269);
contentPane.add(label);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2