Java找不到图像文件的问题

发布于 2024-09-05 05:17:31 字数 2075 浏览 2 评论 0原文

我是一名正在做作业项目的学生。我花了几天时间尝试使用以下代码在我的新 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-09-12 05:17:31

我不是 Applet 人,但将 cup.gifMoveIt.htmlMoveIt.class 放在一起似乎去工作。另外,您要重写 Appletpaint() 方法,而不是 Canvas 的方法。顺便说一句, arg == "Up" 恰好可以工作,因为 Java 字符串是 interned,但 "Up".equals(arg) 是更可靠的谓词。

I'm not the Applet guy, but putting cup.gif alongside MoveIt.html and MoveIt.classseemed to work. Also, you're overriding the paint() method of Applet, not that of Canvas. As an aside, arg == "Up" happens to work because Java strings are interned, but "Up".equals(arg) is the more reliable predicate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文