2014-01-16 17 views
0

我無法添加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"); 
    } 
} 
+2

從'paintComponents'刪除's' –

回答

4

你有你需要重寫paintComponent()法拼寫錯誤,但你卻overrided paintComponents()。在WorldPanel類中更改方法,這有所幫助。

+0

良好的抓住 - 有趣的編程錯誤經常歸結爲一個字符:) – Ewald

-4

覆蓋方法:

public void paint(Graphics g) { 
... 
} 

代替paintComponents在Worldpanel的

+3

這完全錯了!共計 – alex2410

+2

............... –

+0

更改方法名稱來爲我繪製作品。 – KrzyH

0

請注意,如果您使用@Override批註,則編譯器會產生錯誤,因爲此方法不會覆蓋任何內容:)使用此批註是良好的習慣IMO。

@Override 
public void paintComponents(Graphics g) { 
    super.paintComponents(g); 
}