標題:
移動標籤 (二) - 使用執行緒
[打印本頁]
作者:
周政輝
時間:
2018-5-19 11:57
標題:
移動標籤 (二) - 使用執行緒
使用執行緒,改寫 移動標籤 (一) 的程式碼,使標籤移動順暢。
[attach]4120[/attach]
作者:
蔡季樺
時間:
2018-5-19 12:03
本帖最後由 蔡季樺 於 2018-5-26 09:46 編輯
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;
private JLabel label;
/**
* 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);
label = new JLabel("");
label.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\box.png"));
label.setBounds(x, y, 45, 40);
contentPane.add(label);
test();
}
public void test()
{
Thread t = new Thread()
{
public void run()
{
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-23 18:46
本帖最後由 蔡庭豪 於 2018-5-26 10:53 編輯
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 MBS2 extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MBS2 frame = new MBS2();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MBS2() {
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 lblNewLabel = new JLabel("New label");
lblNewLabel.setIcon(new ImageIcon(MBS2.class.getResource("/bbs/istak/org/tw/box.png")));
lblNewLabel.setBounds(10, 10, 50, 50);
contentPane.add(lblNewLabel);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent arg0) {
int x = lblNewLabel.getLocation().x;
int y = lblNewLabel.getLocation().y;
MBS2thread mbs2t = new MBS2thread(x,y,arg0.getKeyCode(),lblNewLabel);
mbs2t.run(x,y,arg0.getKeyCode(),lblNewLabel);
}
});
}
}
複製代碼
package bbs.istak.org.tw;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
public class MBS2thread {
private int x;
private int y;
private int keyCode;
private JLabel Label;
public MBS2thread(int x, int y, int keyCode,JLabel Label) {
this.x = x;
this.y = y;
this.keyCode = keyCode;
this.Label = Label;
}
public void run(int x,int y,int keyCode,JLabel Label){
if(keyCode == KeyEvent.VK_UP)
y-=10;
if(keyCode == KeyEvent.VK_DOWN)
y+=10;
if(keyCode == KeyEvent.VK_RIGHT)
x+=10;
if(keyCode == KeyEvent.VK_LEFT)
x-=10;
Label.setLocation(x, y);
}
}
複製代碼
作者:
張健勳
時間:
2018-5-26 10:32
本帖最後由 張健勳 於 2018-5-26 10:54 編輯
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 = 100, y = 100;
private JLabel lb;
/**
* 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);
lb = new JLabel("");
lb.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/box.png")));
lb.setBounds(x, y, 45, 40);
contentPane.add(lb);
rL();
}
public void rL() {
Thread thread = new Thread() {
public void run() {
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode() == KeyEvent.VK_UP);
switch(e.getKeyCode())
{
case KeyEvent.VK_UP:
y-=5;break;
case KeyEvent.VK_DOWN:
y+=5;break;
case KeyEvent.VK_LEFT:
x-=5;break;
case KeyEvent.VK_RIGHT:
x+=5;break;
default:
break;
}
lb.setLocation(x, y);
}
});
}
};
}
}
複製代碼
作者:
林侑成
時間:
2018-5-26 10:55
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 QWER extends JFrame {
private JPanel contentPane;
private int x=150,y=150;
private JLabel label;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
QWER frame = new QWER();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public QWER() {
setTitle("\u79FB\u52D5\u6A19\u7C64\u7DF4\u7FD2");
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);
label = new JLabel("");
label.setIcon(new ImageIcon("C:\\Users\\student\\Downloads\\box.png"));
label.setBounds(x, y, 45, 40);
contentPane.add(label);
test();
}
public void test()
{
Thread t = new Thread()
{
public void run()
{
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 11:01
package tw.kuas.eu.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;
private JLabel label;
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();
}
}
});
}
public Main() {
setTitle("\u79FB\u52D5\u6A19\u7C64\u7DF4\u7FD2");
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);
label = new JLabel("");
label.setIcon(new ImageIcon(Main.class.getResource("/tw/kuas/eu/tw/box.png")));
label.setBounds(150, 128, 54, 62);
contentPane.add(label);
test();
}
public void test()
{
Thread t = new Thread()
{
public void run()
{
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 11:07
本帖最後由 黃茂勛 於 2018-5-26 11:21 編輯
[code]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 java.awt.Color;
import javax.swing.border.LineBorder;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
public class Main extends JFrame {
private JPanel contentPane;
private JLabel lblNewLabel;
/**
* 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, 495, 398);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblNewLabel = new JLabel("");
lblNewLabel.setBounds(150, 75, 50, 50);
lblNewLabel.setIcon(new ImageIcon(Main.class.getResource("/bbs/istak/org/tw/box.png")));
contentPane.add(lblNewLabel);
addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent arg0) {
int x=lblNewLabel.getLocation().x;
int y=lblNewLabel.getLocation().y;
RunPanel rp = new RunPanel(x,y,arg0.getKeyCode(),lblNewLabel);
rp.start();
}
});
}
}
複製代碼
package bbs.istak.org.tw;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class RunPanel extends Thread{
int x=0,y=0,keycode=0;
JLabel lblNewLabel = null;
RunPanel(int x, int y, int keycode, JLabel lblNewLabel)
{
this.x = x;
this.y = y;
this.keycode = keycode;
this.lblNewLabel = lblNewLabel;
}
public void run()
{
switch(this.keycode)
{
case 38:
y-=5;
break;
case 40:
y+=5;
break;
case 37:
x-=5;
break;
case 39:
x+=5;
break;
default:
break;
}
lblNewLabel.setLocation(this.x, this.y);
}
}
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2