public class ImageButton extends JComponent{
private Image img;
private String url=new String("");
private JFileChooser chooser=new JFileChooser();
public ImageButton(String imageName){
chooser.setCurrentDirectory(new File("."));
url=chooser.getCurrentDirectory().toString() ;
url=url.replace('\\','/');
url=url+"/pic/"+imageName;
img=Toolkit.getDefaultToolkit().getImage(url);
}
public void paintComponent(Graphics g){
g.drawImage(img,0,0,this);
}
}
然后我在主窗口的构造函数中写下如下代码:
imgBtn=new ImageButton("earth.gif");
contentPane=(JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
imgPanel1.add(imgBtn);
contentPane.add(imgPanel1, new XYConstraints(10, 5, 178, 143));
为什么无法显示图形??请指教
private Image img;
private String url=new String("");
private JFileChooser chooser=new JFileChooser();
public ImageButton(String imageName){
chooser.setCurrentDirectory(new File("."));
url=chooser.getCurrentDirectory().toString() ;
url=url.replace('\\','/');
url=url+"/pic/"+imageName;
img=Toolkit.getDefaultToolkit().getImage(url);
}
public void paintComponent(Graphics g){
g.drawImage(img,0,0,this);
}
}
然后我在主窗口的构造函数中写下如下代码:
imgBtn=new ImageButton("earth.gif");
contentPane=(JPanel) this.getContentPane();
contentPane.setLayout(xYLayout1);
imgPanel1.add(imgBtn);
contentPane.add(imgPanel1, new XYConstraints(10, 5, 178, 143));
为什么无法显示图形??请指教
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货