Java多线程移动的小球
importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Graphics;importjava.a...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MoveBall02 extends JFrame {
private JButton jbtu = null;
private Ball02 ball = null;
Thread t = null;
public static void main(String[] args) {
new MoveBall02();
}
public MoveBall02() {
this.setTitle("移动的小球");
this.setSize(600, 500);
this.setLocation(100, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
ball = new Ball02(40, 40);
this.add(ball);
t = new Thread(ball);
jbtu = new JButton("点击移动小球");
this.add(jbtu, BorderLayout.SOUTH);
jbtu.addActionListener(new MyActionListener());
this.setVisible(true);
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jbtu) {
// 响应鼠标点击事件
if(!t.isAlive())
t.start();
}
}
}
}
@SuppressWarnings("serial")
class Ball02 extends JPanel implements Runnable {
private int x, y;
public Ball02(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(x, y, 40, 40);
}
@Override
public void run() {
try {
for(int i=0; i<20; i++) {
x = y += 3;
this.repaint();
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
"AWT-EventQueue-0" java.lang.IllegalThreadStateException
错误是:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
实在是不知道是怎么回事了,
JDK1.8
Win732位
Eclipse 版本
Eclipse Java EE IDE for Web Developers.
Version: Kepler Service Release 2
Build id: 20140224-0627 展开
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MoveBall02 extends JFrame {
private JButton jbtu = null;
private Ball02 ball = null;
Thread t = null;
public static void main(String[] args) {
new MoveBall02();
}
public MoveBall02() {
this.setTitle("移动的小球");
this.setSize(600, 500);
this.setLocation(100, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
ball = new Ball02(40, 40);
this.add(ball);
t = new Thread(ball);
jbtu = new JButton("点击移动小球");
this.add(jbtu, BorderLayout.SOUTH);
jbtu.addActionListener(new MyActionListener());
this.setVisible(true);
}
class MyActionListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jbtu) {
// 响应鼠标点击事件
if(!t.isAlive())
t.start();
}
}
}
}
@SuppressWarnings("serial")
class Ball02 extends JPanel implements Runnable {
private int x, y;
public Ball02(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(x, y, 40, 40);
}
@Override
public void run() {
try {
for(int i=0; i<20; i++) {
x = y += 3;
this.repaint();
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
"AWT-EventQueue-0" java.lang.IllegalThreadStateException
错误是:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalThreadStateException
实在是不知道是怎么回事了,
JDK1.8
Win732位
Eclipse 版本
Eclipse Java EE IDE for Web Developers.
Version: Kepler Service Release 2
Build id: 20140224-0627 展开
展开全部
同一个Thread不能start两次
这样改下
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class MoveBall02 extends JFrame {
private JButton jbtu = null;
private Ball02 ball = null;
Thread t = null;
public static void main(String[] args) {
new Test();
}
public Test() {
this.setTitle("移动的小球");
this.setSize(600, 500);
this.setLocation(100, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
ball = new Ball02(40, 40);
this.add(ball);
t = new Thread(ball);
jbtu = new JButton("点击移动小球");
this.add(jbtu, BorderLayout.SOUTH);
jbtu.addActionListener(new MyActionListener());
this.setVisible(true);
}
class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jbtu) {
// 响应鼠标点击事件
if (!t.isAlive()) {
t = new Thread(ball);
t.start();
}
}
}
}
}
@SuppressWarnings("serial")
class Ball02 extends JPanel implements Runnable {
private int x, y;
public Ball02(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.RED);
g.fillOval(x, y, 40, 40);
}
public void run() {
try {
for (int i = 0; i < 20; i++) {
x = y += 3;
this.repaint();
Thread.sleep(100);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询