標題:
[隨堂測驗] 移動標籤 (一)
[打印本頁]
作者:
周政輝
時間:
2018-5-19 11:24
標題:
[隨堂測驗] 移動標籤 (一)
不限方法 (實作 KeyListener 介面 或 繼承 KeyAdapter 類別),完成如下圖所示之移動標籤練習,以方向鍵控制標籤朝上下左右移動。
[attach]4118[/attach]
圖片素材: [attach]4116[/attach]
作者:
蔡季樺
時間:
2018-5-19 11:54
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Main2 extends JFrame {
private JPanel contentPane;
private int x=150,y=150;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main2 frame = new Main2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Main2() {
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("");
label.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\box.png"));
label.setBounds(x, y, 45, 40);
contentPane.add(label);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyCode() == KeyEvent.VK_UP);
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
y+=10;
}if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y-=10;
}if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=10;
}if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x+=10;
}
label.setLocation(x, y);
}
});
}
}
複製代碼
作者:
陳泓瑜
時間:
2018-5-20 22:39
package tw.kuas.edu.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class BLOCK extends JFrame {
private JPanel contentPane;
private int x=64;
private int y=64;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
BLOCK frame = new BLOCK();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public BLOCK() {
setTitle("\u79FB\u52D5\u6A19\u7C64");
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 block = new JLabel("");
block.setIcon(new ImageIcon("C:\\Users\\redab\\Downloads\\box.png"));
block.setBounds(x, y, 48, 48);
contentPane.add(block);
block.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode()==KeyEvent.VK_LEFT)
x-=15;
if(arg0.getKeyCode()==KeyEvent.VK_RIGHT)
x+=15;
if(arg0.getKeyCode()==KeyEvent.VK_UP)
y-=15;
if(arg0.getKeyCode()==KeyEvent.VK_DOWN)
y+=15;
block.setLocation(x, y);
}
});
}
}
複製代碼
作者:
張健勳
時間:
2018-5-25 19:34
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
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 int x=150,y=150;
/**
* 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() {
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("");
label.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/box.png")));
label.setBounds(x, y, 45, 40);
contentPane.add(label);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyCode() == KeyEvent.VK_UP);
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
y-=10;
}if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y+=10;
}if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=10;
}if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x+=10;
}
label.setLocation(x, y);
}
});
}
}
作者:
林侑成
時間:
2018-5-26 10:10
package asdf;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Asdf extends JFrame {
private JPanel contentPane;
private int x=150,y=150;
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();
}
}
});
}
public Asdf() {
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("");
label.setIcon(new ImageIcon("C:\\Users\\Downloads\\box.png"));
label.setBounds(x, y, 45, 40);
contentPane.add(label);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
System.out.println(arg0.getKeyCode() == KeyEvent.VK_UP);
if(arg0.getKeyCode() == KeyEvent.VK_UP)
{
y+=10;
}if(arg0.getKeyCode() == KeyEvent.VK_DOWN)
{
y-=10;
}if(arg0.getKeyCode() == KeyEvent.VK_LEFT)
{
x-=10;
}if(arg0.getKeyCode() == KeyEvent.VK_RIGHT)
{
x+=10;
}
label.setLocation(x, y);
}
});
}
}
複製代碼
作者:
黃茂勛
時間:
2018-5-26 13:09
package bbs.istak.org.tw;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
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;
/**
* 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() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 476, 375);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("");
lblNewLabel.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/box.png")));
lblNewLabel.setBounds(99, 105, 46, 44);
contentPane.add(lblNewLabel);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
int x = lblNewLabel.getLocation().x;
int y = lblNewLabel.getLocation().y;
switch(arg0.getKeyCode())
{
case 38:
y-=5;
break;
case 40:
y+=5;
break;
case 37:
x-=5;
break;
case 39:
x+=5;
break;
default:
break;
}
lblNewLabel.setLocation(x, y);
}
});
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2