简体   繁体   English

如何通过Java Applet在浏览器中显示信息?

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

I want to display information about the musical composition in the browser via Java applet. 我想通过Java applet在浏览器中显示有关音乐作品的信息。 I use the library beaglebuddy_mp3.jar for id3 tags. 我将库beaglebuddy_mp3.jar用于id3标签。 A folder with files looks like this: 包含文件的文件夹如下所示:

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

In index.html I connect an applet: 在index.html中,我连接一个applet:

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

FirstApplet.class contains the following code: 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();

    }
   } 
}

After starting the index.html file dialog box appears with a warning stating that I run the application at your own risk. 启动index.html文件后,出现对话框,并显示警告,提示我运行该应用程序需要您自担风险。 Then I click "Run", instantly appears and disappears gray square. 然后我单击“运行”,立即出现并消失灰色方块。 On that nothing is displayed. 在那上面什么也没有显示。

Try the following: 请尝试以下操作:

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

And secondly you have to sign your applet code with an official certificate to be able to run it in your web browser. 其次,您必须使用正式证书对applet代码进行签名,才能在Web浏览器中运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM