當我嘗試使用myCustomPanel.add(someComponent)
它不添加...無法項目添加到自定義彩繪的JPanel
這裏是我的自定義JPanel類:
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
/**
*
* @author Jacob
*/
public class OSXMainPanel extends JPanel {
public static final long serialVersionUID = 24362462L;
private Image image;
public OSXMainPanel() {
super.setOpaque(true);
try {
image = javax.imageio.ImageIO.read(new java.net.URL(getClass().getResource("/assets/background.png"), "background.png"));
} catch (Exception e) {}
}
@Override
protected void paintComponent(Graphics g) {
if (isOpaque())
{
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
for(int w = 0; w < this.getWidth(); w = w + 50) {
for(int h = 0; h < this.getHeight(); h = h + 50) {
g.drawImage(image, w, h, 50, 50, this);
}
}
}
}
發佈您的SSCCE(http://sscce.org),證明問題。您發佈的代碼中沒有任何內容會阻止子組件被繪製。如果你看到圖像,那麼你應該看到組件。所以問題在別處。 – camickr 2010-12-10 16:47:52