gpt4 book ai didi

java - 没有小程序发现错误?

转载 作者:行者123 更新时间:2023-12-01 14:44:00 27 4
gpt4 key购买 nike

我正在为一个类(class)编写一个程序,我需要让美国国旗随着国歌升到旗杆上。我有代码,但收到错误消息,提示即使小程序存在,也找不到该小程序。我正在使用 eclipse 。谁能帮我解决我所缺少的东西?

提前致谢...

代码:

@SuppressWarnings("serial")
public class Lab5b extends JApplet {
private AudioClip audioClip;

public Lab5b() {
add(new ImagePanel());

URL urlForAudio = getClass().getResource("audio/us.mid");
audioClip = Applet.newAudioClip(urlForAudio);
audioClip.loop();
}

public void start() {
if (audioClip != null) audioClip.loop();
}

public void stop() {
if (audioClip != null) audioClip.stop();
}

/** Main method */
public static void main(String[] args) {
// Create a frame
JFrame frame = new JFrame("Lab 5");

// Create an instance of the applet
Lab5b applet = new Lab5b();
applet.init();

// Add the applet instance to the frame
frame.add(applet, java.awt.BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Display the frame
frame.setSize(200, 660);
frame.setVisible(true);
}
}

@SuppressWarnings("serial")
class ImagePanel extends JPanel {
private ImageIcon imageIcon = new ImageIcon("image/us.gif");
private Image image = imageIcon.getImage();
private int y = 550;

public ImagePanel() {
Timer timer = new Timer(120, new TimerListener());
timer.start();
}

class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
increaseY();
}
}


public void increaseY() {
if (y > 0) {
y--;
repaint();
}
}

/** Draw image on the panel */
protected void paintComponent(Graphics g) {
super.paintComponent(g);

if (image != null) {
g.fillRect(0, 0, 10, 660);
g.drawImage(image, 11, y, 160, 84, this);
}
}
}

在此处输入代码

最佳答案

需要注意的几点

  • Applet 不会在 main() 方法处开始执行。然而,它是可以使用 Java 解释器执行 applet(通过使用 main() 方法),如果您使用 Frame 扩展您的 class

  • 拥有 init() 方法非常重要,因为它是由浏览器或小程序查看器通知此小程序它已加载到系统中。它总是在第一次之前被调用调用 start 方法。

  • JFrameJApplet 都是顶级容器,而不是将applet添加到frame,我宁愿创建一个对象JPanel 的,因为它可以添加到 JFrame/JApplet 中。在你的这种情况只需将 ImagePanel 添加到任一顶级容器即可。

  • I/O 流没有为小程序提供太多范围。

  • 小程序不可能访问用户上的文件硬盘。

了解更多 here

关于java - 没有小程序发现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15649334/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com