Java找不到图像文件的问题
我是一名正在做作业项目的学生。我花了几天时间尝试使用以下代码在我的新 Windows 7 笔记本电脑上显示图像。我编译了它并在我的旧 XP 电脑上运行它,它工作了!我真的很想使用我的笔记本电脑。关于如何让它显示图像有什么建议吗? java代码。 HTML 和图像都位于我的闪存驱动器上的同一子目录中。我尝试将它们移动到 c:Program Files (x86)\Java\jdk1.5.0_02\bin 目录,但仍然不起作用。
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;
public class MoveIt extends Applet implements ActionListener
{
// set variables and componets
private Image cup;
Panel keypad = new Panel();
public int top = 15;
public int left = 15;
private Button keysArray[];
public void init()
{
cup = getImage(getDocumentBase(), "cup.gif");
Canvas myCanvas = new Canvas();
keysArray = new Button[5];
setLayout(new BorderLayout(5,5));
setBackground(Color.blue);
// set up keypad layout
keypad.setLayout(new BorderLayout(0,0));
keysArray[0] = new Button("Up");
keysArray[1] = new Button("Left");
keysArray[2] = new Button("Center");
keysArray[3] = new Button("Right");
keysArray[4] = new Button("Down");
// add buttons to the keypad panel
keypad.add(keysArray[0], BorderLayout.NORTH);
keysArray[0].addActionListener(this);
keypad.add(keysArray[1], BorderLayout.EAST);
keysArray[1].addActionListener(this);
keypad.add(keysArray[2], BorderLayout.CENTER);
keysArray[2].addActionListener(this);
keypad.add(keysArray[3], BorderLayout.WEST);
keysArray[3].addActionListener(this);
keypad.add(keysArray[4], BorderLayout.SOUTH);
keysArray[4].addActionListener(this);
// add canvas and keypad to the BorderLayout
add(myCanvas, BorderLayout.NORTH);
add(keypad, BorderLayout.SOUTH);
}
public void paint(Graphics g)
{
g.drawImage( cup, left, top, this );
}
public void actionPerformed(ActionEvent e)
{
// test for menu item clicks
String arg = e.getActionCommand();
if (arg == "Up")
top -=15;
else
if (arg == "Down")
top +=15;
else
if (arg == "Left")
left -=15;
else
if (arg == "Right")
left +=15;
else
{
top = 60;
left =125;
}
repaint();
}
}
I am a student working on a homework project. I spent DAYS trying to get the following code to display an image on my new windows 7 laptop. I compiled it and ran it on my old xp pc and it worked! I really want to use my laptop. Any suggestions on how to get it to display the image? The java code. HTML and immage are all in the same sub directory on my flash drive. I tried moving them to the c:Program Files (x86)\Java\jdk1.5.0_02\bin directory but it still didn't work.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.image.*;
public class MoveIt extends Applet implements ActionListener
{
// set variables and componets
private Image cup;
Panel keypad = new Panel();
public int top = 15;
public int left = 15;
private Button keysArray[];
public void init()
{
cup = getImage(getDocumentBase(), "cup.gif");
Canvas myCanvas = new Canvas();
keysArray = new Button[5];
setLayout(new BorderLayout(5,5));
setBackground(Color.blue);
// set up keypad layout
keypad.setLayout(new BorderLayout(0,0));
keysArray[0] = new Button("Up");
keysArray[1] = new Button("Left");
keysArray[2] = new Button("Center");
keysArray[3] = new Button("Right");
keysArray[4] = new Button("Down");
// add buttons to the keypad panel
keypad.add(keysArray[0], BorderLayout.NORTH);
keysArray[0].addActionListener(this);
keypad.add(keysArray[1], BorderLayout.EAST);
keysArray[1].addActionListener(this);
keypad.add(keysArray[2], BorderLayout.CENTER);
keysArray[2].addActionListener(this);
keypad.add(keysArray[3], BorderLayout.WEST);
keysArray[3].addActionListener(this);
keypad.add(keysArray[4], BorderLayout.SOUTH);
keysArray[4].addActionListener(this);
// add canvas and keypad to the BorderLayout
add(myCanvas, BorderLayout.NORTH);
add(keypad, BorderLayout.SOUTH);
}
public void paint(Graphics g)
{
g.drawImage( cup, left, top, this );
}
public void actionPerformed(ActionEvent e)
{
// test for menu item clicks
String arg = e.getActionCommand();
if (arg == "Up")
top -=15;
else
if (arg == "Down")
top +=15;
else
if (arg == "Left")
left -=15;
else
if (arg == "Right")
left +=15;
else
{
top = 60;
left =125;
}
repaint();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是
Applet
人,但将cup.gif
与MoveIt.html
和MoveIt.class
放在一起似乎去工作。另外,您要重写Applet
的paint()
方法,而不是Canvas
的方法。顺便说一句,arg == "Up"
恰好可以工作,因为 Java 字符串是 interned,但"Up".equals(arg)
是更可靠的谓词。I'm not the
Applet
guy, but puttingcup.gif
alongsideMoveIt.html
andMoveIt.class
seemed to work. Also, you're overriding thepaint()
method ofApplet
, not that ofCanvas
. As an aside,arg == "Up"
happens to work because Java strings are interned, but"Up".equals(arg)
is the more reliable predicate.