当前位置: 代码迷 >> Eclipse >> java小程序改错解决办法
  详细解决方案

java小程序改错解决办法

热度:48   发布时间:2016-04-23 13:30:35.0
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);    }}
  相关解决方案