2011-09-28 86 views
0

我試圖在GridBagLayout單元格中添加一個圖像。但我無法實現它。在awt中添加圖像

public class imageInAwt extends Applet{ 
    Panel p; 
    BufferedImage img; 
    public void init(){ 
    setLayout(new GridBagLayout()); 
    GridBagConstraints gbc =new GridBagConstraints(); 
    gbc.gridx = 0; 
    gbc.gridy = 0; 
    try { 
     img = ImageIO.read(new File("settings.gif")); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    add(new junaid(img),gbc); 
    gbc.gridy = GridBagConstraints.RELATIVE; 
    add(new Label("Junaid"),gbc); 
    } 
} 

    class junaid extends Canvas { 
     Image img; 
     public junaid(Image img) { 
     this.img = img; 
     } 
     public void paint (Graphics g) { 
      BufferedImage image = null; 
       g.drawImage(image, 2, 3, null); 
       g.drawString("Junaid", 22, 16); 
     } 
     public void setImage (Image img){ 
     this.img = img; 
     } 
    } 

請大家幫忙。

在此先感謝

+0

什麼是HTML的位置和內容,以及圖像的位置?該applet是否可供我們訪問?在什麼地址/網址? –

+0

小程序類位於何處? – adatapost

+0

這是功課嗎? –

回答

0

使用Applet.getImage() Applet類的方法。

Image img=getImage(getCodeBase(),"settings.gif"); 
+0

仍然沒有工作,我的圖像在res文件夾中。我也試過改變Image的路徑img = getImage(getCodeBase(),「res/settings.gif」); – user966682

1
public void paint (Graphics g) { 
     BufferedImage image = null; 
      g.drawImage(image, 2, 3, null); 
      g.drawString("Junaid", 22, 16); 
    } 

咦?!?

public void paint (Graphics g) { 
      g.drawImage(img, 2, 3, this); 
      g.drawString("Junaid", 22, 16); 
    } 
+0

爲了更快得到更好的幫助,請發佈[SSCCE](http://pscode.org/sscce.html)。 –