首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

java小程序改错解决办法

2012-05-20 
java小程序改错写了一个窗口中有一个小球,当碰到边框时球改变颜色。但是现在写的程序只能显现出边框,不能显

java小程序改错
写了一个窗口中有一个小球,当碰到边框时球改变颜色。但是现在写的程序只能显现出边框,不能显示出球。求各位看看指出下错误啊

import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;

class Mywindow extends Frame {
Mywindow (){
super();
setSize(350,350);
setVisible(true);
setBackground(Color.BLACK);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}



public class lsnfinal extends Frame implements Runnable{
int x=0,y=160;
int dx=5,dy=0;
int rgb=0;
Color color=new Color((int)(Math.random()*0xFFFFFF));
Mywindow win;

public static void main(String[] args){
lsnfinal workStart=new lsnfinal();
}

public lsnfinal(){
Mywindow win=new Mywindow();
new Thread(this).start();
}
public void doColor(){
rgb=(int)(Math.random()*0xFFFFFF);
color=new Color(rgb);
}
public void run(){
while(true){
if(x<=0) {dx=5;doColor();} 
else if((x+50)>=win.getWidth()) {dx=-5;doColor();}
if(y<=0) {dy=5;doColor();}
else if((y+50)>=win.getHeight()) {dy=-5;doColor();}
x=x+dx;
y=y+dy; 
repaint();
try{Thread.sleep(250);}
catch(InterruptedException e) {;}
}
}

public void paint(Graphics g){
g.setColor(color);
g.fillOval(x,y,50,50);
}
}






[解决办法]

Java code
import java.awt.*;import java.awt.event.*;import java.awt.Graphics;public class lsnfinal extends Frame implements Runnable {    int x = 60, y = 160;    int dx = 5, dy = 0;    int rgb = 0;    Color color = new Color((int)(Math.random() * 0xFFFFFF));    public static void main(String[] args) {        new lsnfinal();    }    public lsnfinal(){        setSize(350, 350);        setBackground(Color.gray);        this.setLayout(null);        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });        setVisible(true);        new Thread(this).start();    }    public void doColor() {        rgb = (int)(Math.random()*0xFFFFFF);        color = new Color(rgb);    }    public void run() {        while (true) {            if (x <= 0) {                dx = 5;                doColor();            } else if ((x + 50) >= this.getWidth()) {                dx = -5;                doColor();            }            if (y <= 0) {                dy = 5;                doColor();            }else if ((y + 50) >= this.getHeight()){                dy = -5;                doColor();            }            x = x + dx;            y = y + dy;            try {                Thread.sleep(50);                repaint();            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }    public void paint(Graphics g) {        Color c = g.getColor();        g.setColor(color);        g.fillOval(x, y, 50, 50);        g.setColor(c);    }}
[解决办法]
或者
Java code
import java.awt.*;import java.awt.event.*;import java.awt.Graphics;class Mywindow extends Frame {    Frame f;    Mywindow(Frame frame) {        this.f = frame;        setSize(350, 350);        setBackground(Color.black);        this.setLayout(null);        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });        setVisible(true);    }    public void paint(Graphics g){        f.paint(g);    }}public class lsnfinal extends Frame implements Runnable {    int x = 60, y = 160;    int dx = 5, dy = 0;    int rgb = 0;    Color color = new Color((int)(Math.random() * 0xFFFFFF));    Mywindow win = null;    public static void main(String[] args) {        new lsnfinal();    }    public lsnfinal(){        win = new Mywindow(this);        new Thread(this).start();    }    public void doColor() {        rgb = (int)(Math.random()*0xFFFFFF);        color = new Color(rgb);    }    public void run() {        while (true) {            if (x <= 0) {                dx = 5;                doColor();            } else if ((x + 50) >= win.getWidth()) {                dx = -5;                doColor();            }            if (y <= 0) {                dy = 5;                doColor();            }else if ((y + 50) >= win.getHeight()){                dy = -5;                doColor();            }            x = x + dx;            y = y + dy;            try {                Thread.sleep(250);                win.repaint();            } catch (InterruptedException e) {                e.printStackTrace();            }        }    }        public void paint(Graphics g) {        Color c = g.getColor();        g.setColor(color);        g.fillOval(x, y, 50, 50);        g.setColor(c);    }} 

热点排行