java repaint()结果没反应
importjava.awt.*;importjavax.swing.*;publicclassTest_2{publicstaticvoidmain(String[]a...
import java.awt.*;
import javax.swing.*;
public class Test_2{
public static void main(String[] args){
BallMove bm=new BallMove();
Thread t=new Thread(bm);
t.start();
}
}
class BallMove extends JFrame implements Runnable{
int a=200,b=200;
Ball ball=new Ball(a,b);
BallMove(){
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(ball);
}
public void run(){
//问题在下面函数,为啥不能进行repaint函数
while(true){
a--;
b--;
try{Thread.sleep(30);}
catch(Exception e){System.out.println(e);}
ball.repaint();
}
}
}
class Ball extends JPanel{
int x=0,y=0;
Ball(int x,int y){
this.x=x;
this.y=y;
}
public void paint(Graphics g){
super.paint(g);
g.setColor(new Color(100,30,30));
g.fillOval(x,y,10,10);
}
} 展开
import javax.swing.*;
public class Test_2{
public static void main(String[] args){
BallMove bm=new BallMove();
Thread t=new Thread(bm);
t.start();
}
}
class BallMove extends JFrame implements Runnable{
int a=200,b=200;
Ball ball=new Ball(a,b);
BallMove(){
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(ball);
}
public void run(){
//问题在下面函数,为啥不能进行repaint函数
while(true){
a--;
b--;
try{Thread.sleep(30);}
catch(Exception e){System.out.println(e);}
ball.repaint();
}
}
}
class Ball extends JPanel{
int x=0,y=0;
Ball(int x,int y){
this.x=x;
this.y=y;
}
public void paint(Graphics g){
super.paint(g);
g.setColor(new Color(100,30,30));
g.fillOval(x,y,10,10);
}
} 展开
2个回答
展开全部
public class Test_2 {
public static void main(String[] args) {
BallMove bm = new BallMove();
}
}
class BallMove extends JFrame implements Runnable {
int a = 300, b = 200;
Ball ball = new Ball(a, b);
Thread t;
BallMove() {
t = new Thread(this);
t.start();
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(ball);
}
@Override
public void run() {
while(true) {
ball.x--;
ball.y--;
try {
ball.repaint();
Thread.sleep(50);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
这么渣拿枝改就可行了。如敏道友是不是以前用C的。java里a--; b--;这两句不影响ball的实例变量x和y的值的敏尺。
public static void main(String[] args) {
BallMove bm = new BallMove();
}
}
class BallMove extends JFrame implements Runnable {
int a = 300, b = 200;
Ball ball = new Ball(a, b);
Thread t;
BallMove() {
t = new Thread(this);
t.start();
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(ball);
}
@Override
public void run() {
while(true) {
ball.x--;
ball.y--;
try {
ball.repaint();
Thread.sleep(50);
} catch (Exception e) {
System.out.println(e);
}
}
}
}
这么渣拿枝改就可行了。如敏道友是不是以前用C的。java里a--; b--;这两句不影响ball的实例变量x和y的值的敏尺。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询