同一个Thread不能start两次
这样改下
import java.awt.BorderLayoutimport 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()
}
}
}
import javax.microedition.lcdui.Displayimport javax.microedition.midlet.MIDlet
import javax.microedition.midlet.MIDletStateChangeExceptionimport com.sun.midp.lcdui.DisplayDeviceAccess
public class Main extends MIDlet {
public static Display display
public static GameCanvas Game
public static MIDlet mid
public Main() {
Game=new GameCanvas()
display=Display.getDisplay(this)
mid=this
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException {display.setCurrent(Game)}
}
Canvas类=========================import java.util.Randomimport javax.microedition.lcdui.Canvas
import javax.microedition.lcdui.Graphics
public class GameCanvas extends Canvas implements Runnable{
private int BallX=0
private int BallY=0
private int BallW=20
private int BallH=20
private boolean BallRun=true
private byte BallDir=0
private final byte Left=1
private final byte Right=2
private final int ScreenWith=150
private final int ScreenHight=200
private final int Speed = 5
Random rand
public GameCanvas(){
rand=new Random()
int state=rand.nextInt()%2
BallDir=(byte)(state==0?Left:Right)
BallX=rand.nextInt()%240
BallY=rand.nextInt()%320
new Thread(this).start()
this.setFullScreenMode(true)
}
protected void paint(Graphics g) {
g.setColor(0)
g.fillRect(0, 0,this.getWidth(), this.getHeight())
g.setColor(255, 255,255)
g.fillArc(BallX, BallY,BallW, BallH, 0,360)
} public void BallRun(){
switch(BallDir){
case Left:
if(BallX>0){BallX-=Speed}
else{BallDir=Right}break
case Right:
if(BallX<ScreenWith){BallX+=Speed}
else{BallDir=Left}break
}
if(BallY>=0&&BallRun){
BallY-=Speed
}else{BallRun=false}
if(BallY<=ScreenHight&&!BallRun){
BallY+=Speed
}else{BallRun=true}
}
public void run() {
while(true){
BallRun()
this.repaint()
try{Thread.sleep(100)<br> }catch(Exception ex){ex.printStackTrace()}
} }}
this.posX=(random.nextInt()>>>1)%(this.getWidth()-20)+10this.posY=(random.nextInt()>>>1)%(this.getHeight()-20)+10
可以改成
this.posY=(posY+10)%(this.getHeight()-20)+10