我正在为一个班级编写一个程序,我需要让美国国旗随着国歌升到旗杆上。我有代码,但收到错误消息,提示即使小程序存在,也找不到该小程序。我正在使用日食。谁能帮我解决我所缺少的东西?
提前致谢...
代码:
@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);
}
}
}
有几点需要注意
Applets
不要在 main()
方法处开始执行。然而,它是
可以使用 Java 解释器执行 applets
(通过
使用 main()
方法)如果您用 class
扩展 Frame
。拥有
init()
方法很重要,因为它是由
浏览器或小程序查看器通知此小程序它已
加载到系统中。它总是在第一次之前被调用
调用了 start 方法。JFrame
和 JApplet
都是顶级容器,而不是
将 applet
添加到 frame
,我宁愿创建一个对象
JPanel
,因为它可以添加到 JFrame
/JApplet
。在你的
这种情况只需将 ImagePanel
添加到任一顶级容器即可。I/O 流没有为
applets
提供太多范围。 applet
无法访问用户上的文件
硬盘。阅读更多这里