簡體   English   中英

如何通過Java Applet在瀏覽器中顯示信息?

[英]How to display the information in a browser via Java Applet?

我想通過Java applet在瀏覽器中顯示有關音樂作品的信息。 我將庫beaglebuddy_mp3.jar用於id3標簽。 包含文件的文件夾如下所示:

applet
 - index.html
 - FirstApplet.class
 - beaglebuddy_mp3.jar

在index.html中,我連接一個applet:

<applet code="FirstApplet.class" archive="beaglebuddy_mp3.jar" width="500" height="500"></applet>

FirstApplet.class包含以下代碼:

import java.applet.Applet;
import java.awt.Graphics;
import java.io.File;
import java.io.IOException;

import com.beaglebuddy.mp3.MP3;

public class FirstApplet extends Applet{

public void paint(Graphics g){
    try {
        MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
        g.drawString(mp3.getBand() +" "+mp3.getTitle(), 20, 20);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
   } 
}

啟動index.html文件后,出現對話框,並顯示警告,提示我運行該應用程序需要您自擔風險。 然后我單擊“運行”,立即出現並消失灰色方塊。 在那上面什么也沒有顯示。

請嘗試以下操作:

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import java.io.File;
import java.io.IOException;
import com.beaglebuddy.mp3.MP3;


public class FirstApplet extends JApplet {

    public void init() {

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {

                    MP3 mp3 = new MP3("D:\\Music\\abc.mp3");
                    JLabel label = new JLabel(mp3.getBand() +" "+mp3.getTitle());
                    add(label);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

其次,您必須使用正式證書對applet代碼進行簽名,才能在Web瀏覽器中運行它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM