我正在使用Headfirst java这本书,并且我已经编写了一个我认为可以很好编译的程序。但是当创建窗口时,背景或椭圆形没有出现。
import javax.swing.*;
import java.awt.*;
public class setup {
public static void main(String[] args) {
JFrame f = new JFrame();
System.out.println("Created Frame");
JPanel myJPan = new JPanel();
System.out.println("Created Panel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,300);
System.out.println("Set Size");
f.setLocationRelativeTo(null);
f.setContentPane(myJPan);
f.setVisible(true);
System.out.println("Made Visible");
myJPan.repaint();
}
// @Override ???
// "protected void" ??
public void paintComponent(Graphics g) {
// super.paintComponent(); ???
g.fillRect(0,0,300,300);
System.out.println("painted");
int red = (int) (Math.random()*255);
int green = (int) (Math.random()*255);
int blue = (int)(Math.random()*255);
System.out.println("Got Random Colors");
Color randomColor = new Color(red, green, blue);
g.setColor(randomColor);
System.out.println("Set Random Colors");
g.fillOval(70,70,100,100);
System.out.println("Filled Oval");
}
}