2013-03-26 126 views
0

我正在寫一個課程,我需要讓美國國旗將國旗伸向國歌。我有代碼,但得到一個錯誤,即使它在那裏找不到applet。我正在使用eclipse。任何人都可以幫我解決我失蹤的問題嗎?沒有applet發現錯誤?

在此先感謝...

代碼:

@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); 
     } 
    } 
} 

在此處輸入代碼

回答

1

有幾件事情需要注意

  • Applets不要在main()方法開始執行。但是,如果使用Frame擴展您的class,則可以通過使用Java解釋器(通過 使用main()方法)執行applets的 。

  • init()方法,因爲它是由 瀏覽器或applet viewer調用,通知此applet它已經 加載到系統中是很重要的。它始終在第一次調用start方法之前調用 。

  • JFrameJApplet都是頂層容器和代替 添加appletframe,我寧願創建一個對象的JPanel ,因爲它可以被添加到兩個JFrame/JApplet。在您的 案例中,只需將ImagePanel添加到頂級容器中的任意一個。

  • I/O流不爲applets提供很大空間。

  • applet不可能訪問用戶 硬盤上的文件。

更多here

+0

*「這是不可能的一個小程序訪問用戶硬盤上的文件。」 *是的,它是可能的。有兩種方式(在JRE中缺少安全漏洞)。 – 2013-03-27 00:28:38