我無法添加jpanel我已經騎到框架。它沒有顯示它。我通過獲取內容窗格添加它,但它不起作用,我該怎麼做。 我的問題在哪裏。 預先感謝您我不能添加JPanel我騎過去的框架。它不顯示
import java.awt.*;
import javax.swing.*;
public class BigBang {
JFrame worldFrame;
WorldPanel worldPanel;
int worldX=800;
int worldY=600;
public void draw(){
worldFrame=new JFrame("world");
worldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
worldPanel=new WorldPanel(new Dimension(800,600));
worldFrame.setLayout(new FlowLayout());
worldFrame.setSize(worldX,worldY);
worldFrame.getContentPane().add(worldPanel);
System.out.println("done");
worldFrame.setVisible(true);
}
public static void main(String[] args) {
BigBang b=new BigBang();
b.draw();
}
}
class WorldPanel extends JPanel{
private static final long serialVersionUID = 1L;
Graphics graphic;
WorldPanel(Dimension d){
setPreferredSize(d);
}
public void paintComponents(Graphics g) {
super.paintComponents(g);
System.out.println("it is in");
graphic=g;
setBackground(Color.black);
int firstx=60;
int firsty=90;
g.setColor(Color.white);
g.fillOval(firstx, firsty, 30, 30);
System.out.println("here");
}
}
從'paintComponents'刪除's' –