0
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Exercise2 extends JFrame implements ActionListener, Runnable{
public int x = 20;
public Exercise2(){
setSize(400, 200);
setTitle("Moving Car");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JButton move = new JButton("Move the car");
move.addActionListener(this);
add(move , BorderLayout.SOUTH);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.drawRect(x, 80, 80, 50);
g.drawOval(x, 130, 30, 30);
g.drawOval(x+50, 130, 30, 30);
}
public void actionPerformed(ActionEvent e){
Thread t = new Thread(this);
t.run();
}
public void run(){
for(int i = 0; i < 400; i += 10){
x += 10;
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String []args){
new Exercise2();
}}
這是我第一次在這個網站上提問,所以我提前爲我的錯誤道歉。用線程在GUI中動畫形狀
我目前正在學習線程和即時通訊應該讓汽車按下按鈕,但當我按下按鈕,而不是移動它只是跳過,並在我選定的時間後出現在另一側。 我該如何解決這個問題?
非常感謝你,修復它。我仍然需要了解線程... – ProgrammerAtSea 2014-12-14 18:52:09