繁体   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