为什么内层的paint函数只执行了一次?
Rectangle里面的Paint函数只执行了一次.求原因第一张图是要实现的GUI界面,第二张是输出(只有一行0000说明paint只执行了一次),第三张是运行结果的GUI
附代码:
package Homework3;
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Homework3_2 {
public static class newWindow extends JFrame
{ //定义一个类继承JFrame类
private int data[][];
private int r[];
private Graphics[] R;
public newWindow(String title,int data[][]) {
int wH,wW;
wH=350;
wW=600;
Color color[]={Color.BLACK,Color.BLUE,Color.GRAY};
JFrame window = this; //实例化一个JFrame对象
for(int i=0;i<6;i++)
{
JLabel a=new JLabel(50*i+" "+"--------------------------------------------------------------------------------------------------------------");
a.setBounds(10, wH-(i*50+75), wW, 10);
a.setVisible(true);
window.add(a);
}
for(int i=0;i<4;i++)
{
JLabel b=new JLabel("商品"+(i+1));
b.setBounds(70+100*i, wH-65, 100, 20);
window.add(b);
for(int j=0;j<3;j++)
{
Rectangle rt= null;
int h=data;
int rect[]={100*i+33*j,wH-h-70,33,h};
System.out.println(Arrays.toString(rect));
rt=new Rectangle(rect,color);
window.add(rt);
rt.paintImmediately(rect, rect, rect, rect);
}
}
window.setVisible(true); //使窗体可视
window.setSize(600, 350); //设置窗口大小
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置窗口关闭方式
}
}
static class Rectangle extends JPanel{
private static final long serialVersionUID = 1L;
private int r[];
private Color color;
public Rectangle(int rect[],Color c)
{
this.r=rect;
this.color =c;
this.setVisible(true);
}
public void paint(Graphics g)
{
g.setColor(this.color);
g.fillRect(this.r,this.r,this.r,this.r);
System.out.println("0000000");
}
}
public static void main(String[] args) {
int data[][]={{30,50,30,100},{110,120,130,140},{110,120,130,140}};
new newWindow("111",data);
}
} {:10_266:}有大佬能解答下不
页:
[1]