简体   繁体   中英

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. I use the library beaglebuddy_mp3.jar for id3 tags. A folder with files looks like this:

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

In index.html I connect an applet:

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

FirstApplet.class contains the following code:

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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