標題:
鍵盤事件 (三)
[打印本頁]
作者:
周政輝
時間:
2018-2-24 11:11
標題:
鍵盤事件 (三)
承上個主題,判斷 Shift 鍵是否被按住,使執行時若搭配 Shift 鍵,則上下左右鍵所造成的方向會顛倒。
[attach]3403[/attach]
作者:
蔡季樺
時間:
2018-2-24 11:17
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("baby.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("/bbs/istak/org/tw/icon.png")));
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);addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(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;
}
}
label.setLocation(x,y);
}
});
label.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
}
});
label.setBounds(144, 64, 112, 94);
contentPane.add(label);
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-2-24 11:20
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 MOVE 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 {
MOVE frame = new MOVE();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MOVE() {
setTitle("\u6309\u65B9\u5411\u9375\u79FB\u52D5\u5716\u5F62");
ImageIcon icon = new ImageIcon(MOVE.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(MOVE.class.getResource("baby.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;
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y-=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x+=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x-=5;
}
}
else
{
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
y-=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y+=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x+=5;
}
}
label.setBounds(x,y,100,100);
}
});
}
}
複製代碼
作者:
張健勳
時間:
2018-2-24 11:20
本帖最後由 張健勳 於 2018-3-2 23:12 編輯
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;
}if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
y-=15;
}if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
x+=15;
}if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
x-=15;
}
}else
{
if(e.getKeyCode() == KeyEvent.VK_UP)
{
y-=15;
}if(e.getKeyCode() == KeyEvent.VK_DOWN)
{
y+=15;
}if(e.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=15;
}if(e.getKeyCode() == KeyEvent.VK_RIGHT)
{
x+=15;
}
}
label.setLocation(x,y);
}
});
label.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
}
});
label.setBounds(50, -18, 338, 269);
contentPane.add(label);
}
作者:
黃茂勛
時間:
2018-2-24 11:21
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;
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("baby.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(100, 100, 129, 118);
contentPane.add(lblNewLabel);
addKeyListener(new KeyAdapter() {
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;
}
lblNewLabel.setLocation(x, y);
}
});
}
}
複製代碼
作者:
蔡庭豪
時間:
2018-2-24 11:21
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 udicon extends JFrame {
private JPanel contentPane;
private ImageIcon icon;
private int x=168,y=24;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
udicon frame = new udicon();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public udicon() {
setImageIcon("imageicon_2.png");
icon = new ImageIcon(udicon.class.getResource("imageicon_1.png"));
Image img = icon.getImage().getScaledInstance(81,81,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(168, 24, 81, 81);
label.setLocation(x, y);
contentPane.add(label);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if(e.isShiftDown()){
if(e.getKeyCode()==e.VK_LEFT)
x+=3;
if(e.getKeyCode()==e.VK_UP)
y+=3;
if(e.getKeyCode()==e.VK_RIGHT)
x-=3;
if(e.getKeyCode()==e.VK_DOWN)
y-=3;
label.setLocation(x, y);
}
else{
if(e.getKeyCode()==e.VK_LEFT)
x-=3;
if(e.getKeyCode()==e.VK_UP)
y-=3;
if(e.getKeyCode()==e.VK_RIGHT)
x+=3;
if(e.getKeyCode()==e.VK_DOWN)
y+=3;
label.setLocation(x, y);
}
}});
}
private void setImageIcon(String string) {
// TODO 自動產生的方法 Stub
}
}
複製代碼
作者:
林侑成
時間:
2018-2-24 11:27
package asdf;
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 Asdf extends JFrame {
private JPanel contentPane;
private int x=100;
private int y=100;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Asdf frame = new Asdf();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Asdf() {
setTitle("\u6309\u65B9\u5411\u9375\u79FB\u52D5\u5716\u5F62");
ImageIcon icon = new ImageIcon(Asdf.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(Asdf.class.getResource("baby.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;
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y-=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x+=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x-=5;
}
}
else
{
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
y-=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y+=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=5;
}
if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x+=5;
}
}
label.setBounds(x,y,100,100);
}
});
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2