帮忙解析一下这个Java程序

来源:百度知道 编辑:UC知道 时间:2024/09/21 11:00:39
先顶一个类
import java.awt.*;
import javax.swing.*;

public class ColoredRectangle
{
private int width;
private int height;
private int x;
private int y;
private JFrame window;
private Color colors;

public ColoredRectangle()
{
window = new JFrame("Box Fun");
window.setSize(200,200);

width=40;
height=20;
x=80;
y=90;

colors = Color.BLUE;

window.setVisible(true);
}

public void paint()
{
Graphics g = window.getGraphics();
g.setColor(colors);
g.fillRect(x,y,width,height);
}
}

调用类
import java.io.*;

public class BoxFun
{
public static void main(String[] args) throws IOException
{
ColoredRectangle r1 = new ColoredRectangle();
ColoredRectangle r2 = new ColoredRectangle();

System.out.println("Enter wh

//import java.io.IOException;

public class BoxFun {
public static void main(String[] args) throws IOException {
ColoredRectangle r1 = new ColoredRectangle();
// ColoredRectangle r2 = new ColoredRectangle();
r1.paint();
System.out.println("Enter when ready");
System.in.read(); //我不知道你这IO流是用来干吗。但是。IO程序走到IO流以后就终止了。
//以后的东西不会走了 r1.paint(); r2.paint(); 没用
// r1.paint();
// r2.paint();
}
}

//---------------------------------------

我给你改成这样就行了!!! 还有就是 paint方法是程序自动运行的方法。
如果像你写的那样paint只是个方法的名字。

import java.awt.*;
import javax.swing.*;

public class ColoredRectangle extends JFrame{
private int width;

private int height;

private int x;

private int y;

private Color colors;

public ColoredRectangle() {
super("Box Fun");
setSize(200, 200);