2 回答
TA贡献1818条经验 获得超8个赞
public class Colors extends JFrame {
private static final Random rand = new Random();
public Colors() {
super("Colors");
final JPanel panel = new JPanel() {
public void paint(Graphics draw) {
// draw.setColor(Color.black);
// draw.fillRect(0, 0, 640, 480);
draw.drawString("Colors ", 50, 50);
Color newColor = new Color(40, 60, 80);
draw.setColor(newColor);
draw.drawArc(100, 100, 50, 50, 0, -180);
newColor = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
draw.setColor(newColor);
draw.drawRect(250, 100, 50, 50);
newColor = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
draw.setColor(newColor);
draw.fillRect(450, 200, 50, 50);
}
};
Action action = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
};
Timer timer = new Timer(250, action);
timer.setRepeats(true);
timer.start();
this.setContentPane(panel);
setSize(640, 480);
setVisible(true);
setBackground(Color.white);
}
public static void main(String args[]) {
Colors test = new Colors();
}
}
透明了,你自己填充了一次黑色就黑了。pait在窗体变化的时候就回自动重画。如果你自己调用repaint时候也回重画。
TA贡献1824条经验 获得超5个赞
当组建第一次加载的时候会自动调用paint()
paint是系统回调函数,当你以后在想调用paint的时候,直接写repaint()就可以了,repaint会调用paint。
背景透明?不会啊,JFrame默认是透明的,你加上setVisible(true);
就不会了啊,我这看是黑色的,你自己在看看吧
添加回答
举报
