关于如何使用getImage方法???

MashiMaro2002 2003-04-11 11:02:56
写了一个小程序使用FileDialog对话框打开用户指定的gif图像文件,却发现总是编译不过去,好像是getImage方法中的参数不对,不知道如何修改???请指教!!!
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;

public class ShowGIF
{
public static void main(String args[])
{
new FileFrame();
}
}

class FileFrame extends Frame implements ActionListener
{
Button open,quit;
FileDialog fd;
Label prompt;
Image myImage;
FileFrame()
{
super("获取并显示gif图像文件:");
open=new Button("打开");
quit=new Button("关闭");
prompt=new Label("请按打开按钮指定欲打开gif图片按钮的地址:");
open.addActionListener(this);
quit.addActionListener(this);
setLayout(new FlowLayout());
add(prompt);
add(open);
add(quit);
}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="打开")
{
fd=new FileDialog(this,"打开文件",FileDialog.LOAD);
fd.setDirectory("d:\\temp");//设置文件对话框的基础目录
fd.show();//弹出并显示文件对话框,程序暂停直至用户选定一文件
try{
myImage=getImage(fd.getDirectory(),fd.getFile());//出错!!!是不是参数fd.getDirectory()不对?
repaint();
}
catch(IOException ioe)
{
System.err.println(ioe.toString());
}
}
if(e.getActionCommand()=="关闭")
{
dispose();
System.exit(0);
}
}

public void paint(Graphics g)
{
g.drawImage(myImage,10,10,this);
}
}
j2sdk1.4.1中的调试信息:
G:\My Works\Programs\Java\ShowGIF.java:41: cannot resolve symbol
symbol : method getImage (java.lang.String,java.lang.String)
location: class FileFrame
myImage=getImage(fd.getDirectory(),fd.getFile());//出错!!!是不是参数fd.getDirectory()不对?
^
G:\My Works\Programs\Java\ShowGIF.java:44: cannot resolve symbol
symbol : class IOException
location: class FileFrame
catch(IOException ioe)
^
2 errors
...全文
1225 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiaojian77 2003-04-16
  • 打赏
  • 举报
回复
是我写错了,应该是Frame。
Frame也没有getImage的方法呀。
MashiMaro2002 2003-04-12
  • 打赏
  • 举报
回复
/**
* Returns an <code>Image</code> object that can then be painted on
* the screen. The <code>url</code> argument must specify an absolute
* URL. The <code>name</code> argument is a specifier that is
* relative to the <code>url</code> argument.
* <p>
* This method always returns immediately, whether or not the image
* exists. When this applet attempts to draw the image on the screen,
* the data will be loaded. The graphics primitives that draw the
* image will incrementally paint on the screen.
*
* @param url an absolute URL giving the base location of the image.
* @param name the location of the image, relative to the
* <code>url</code> argument.
* @return the image at the specified URL.
* @see java.awt.Image
*/
public Image getImage(URL url, String name) {
try {
return getImage(new URL(url, name));
} catch (MalformedURLException e) {
return null;
}
}
上面是J2SDK1.4.1说明文档中对getImage方法的说明,getImage方法位于java.applet包中
MashiMaro2002 2003-04-12
  • 打赏
  • 举报
回复
我写的根本就不是JFrame呀!哪里让你认为我写的是swing啦!?
jiaojian77 2003-04-11
  • 打赏
  • 举报
回复
而且,JFrame没有getImage的方法呀。
应该Toolkit.getDefaultToolkit().getImage(fd.getDirectory()+fd.getFile());
你的程序是从applet程序中copy的吧?
geyf 2003-04-11
  • 打赏
  • 举报
回复
getImage(String filename);
GhostKiller 2003-04-11
  • 打赏
  • 举报
回复
关于第二个IOException ,你只需要加载io 包就可以了.
import java.io.*;
因为它是定义在io包里的.

至于第一个错误,我不知道你的函数getImage(...)是在哪里定义的. 或者是哪个类的成员
函数.

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧