问题描述
有3个类1:MyFrame:import java.awt.event.ActionEvent;public class MyFrame extends javax.swing.JFrame {private java.awt.Graphics g;private int x, y;private java.util.Random rand = new java.util.Random();public static MyThread mt;public static MyFrame mf = new MyFrame();public static int num = 3;public void init() {this.setTitle("线程的暂停与继续");this.setSize(700, 700);this.setLayout(new java.awt.FlowLayout());// 添加开始按钮final javax.swing.JButton bu = new javax.swing.JButton("开始");this.add(bu);final javax.swing.JButton bu2 = new javax.swing.JButton("暂停");this.add(bu2);// 添加进度条final javax.swing.JProgressBar probar = new javax.swing.JProgressBar();this.add(probar);this.setDefaultCloseOperation(3);this.setLocationRelativeTo(null);this.setVisible(true);g = this.getGraphics();// 按钮1bu.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {mt = new MyThread(probar);String str = new String();if ("开始".equals(e.getActionCommand())) {for (int i = 0; i < num; i++) {x = rand.nextInt(500) + 100;y = rand.nextInt(500) + 100;BallThread bt = new BallThread(mf, g, x, y);bt.start();}mt.start();bu.setText("停止");}if ("停止".equals(e.getActionCommand())) {mt.stopThread();bu.setText("重新开始");bu2.setText("暂停");}if ("重新开始".equals(e.getActionCommand())) {mt.restart();mt.start();for (int i = 0; i < num; i++) {x = rand.nextInt(500) + 100;y = rand.nextInt(500) + 100;BallThread bt = new BallThread(mf, g, x, y);bt.start();}bu.setText("停止");}}});// 按钮2bu2.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {String str = new String();if ("暂停".equals(e.getActionCommand())) {mt.pauseThread();bu2.setText("继续");}if ("继续".equals(e.getActionCommand())) {mt.continueThread();bu2.setText("暂停");}}});}public static void main(String args[]) {MyFrame mf = new MyFrame();mf.init();}}2.MyThread:public class MyThread extends Thread {public static boolean isFinish = false;// 是否停止public static boolean isPause = false;// 是否暂停private javax.swing.JProgressBar probar;private int value = 0;public MyThread(javax.swing.JProgressBar probar) {this.probar = probar;}public void run() {this.changeValue();}public void changeValue() {while (!isFinish) {// System.out.println("暂停中");while (!isPause) {// System.out.println("运行中");probar.setValue(value);value++;try {Thread.sleep(100);} catch (Exception ef) {ef.printStackTrace();}}try {Thread.sleep(100);} catch (Exception ef) {ef.printStackTrace();}}// System.out.println("结束了");}public void stopThread() {isPause = true;isFinish = true;}public void continueThread() {isPause = false;}public void pauseThread() {isPause = true;}public void restart() {value = 0;isPause = false;isFinish = false;}}3.BallThread:public class BallThread extends Thread {private java.awt.Graphics g;private int x = 20, y = 20;private MyFrame mf = MyFrame.mf;private java.util.Random rand = new java.util.Random();public static int xx[] = new int[30];public static int yy[] = new int[30];public BallThread(MyFrame mf, java.awt.Graphics g, int x, int y) {this.mf = MyFrame.mf;this.g = g;this.x = x;this.y = y;}public BallThread(int x,int y) {this.x = x;this.y = y;}public void run() {changBall();}public synchronized void changBall() {while (!MyThread.isFinish) {//System.out.println("运行了");while (!MyThread.isPause) {//System.out.println("运行了");g.setColor(mf.getBackground());g.fillOval(x, y, 20, 20);x += 20;y += 20;g.setColor(java.awt.Color.BLUE);g.fillOval(x, y, 20, 20);try {Thread.sleep(100);} catch (Exception ef) {ef.printStackTrace();}}}if(MyThread.isFinish) {g.setColor(mf.getBackground());g.fillOval(x, y, 20, 20);}}public int getX() {return this.x;}public int getY() {return this.y;}}若将MyFrame中的num设置为3,则重复执行开始、停止、重新开始,可能会出现球擦不干净的情况,若num设置为1,则每次运行球都擦得干净。不知道是什么原因,期待高手讲解。 问题补充:cuiran 写道
解决方案
你的程序调用的时候是,每次生成一个 BallThread bt = new BallThread(mf, g, x, y); bt.start(); 然后就开始运行,这样在运行的过程中就容易出错,你可以先产生对象然后在运行.
解决方案二:
你num为3就相当于创建了3个BallThread的线程,创建了3个球的运行.但你在做 g.setColor(mf.getBackground()); g.fillOval(x, y, 20, 20); 当球走完后对之前的进行擦除时候,此时已经乱了,因为此时的x,y值它不知道是哪一个球的x,y.你可以写个bean.里面包含ball的num和对应的x,y值,然后在画ball的下个轨迹的时候用变量对其之前的进行保存,这样就对变量中的x,y值进行擦除.思路是这样,我现在还在修改你的代码.