简体   繁体   中英

Java Applet + HTML

I have a problem with embedding my applet in html file. I've got "no class def found error" in browser. This is the simple applet connected with MySQL database. This is the code:

public class Nowy extends JApplet {

JPanel panel;
JButton count, end;
JLabel result;
int score;
String name = "Matthew";

    @Override
    public void init() {
        panel = new JPanel();
        panel.setLayout(null);
        add(panel);

        result = new JLabel("0");
        result.setBounds(10,10,100,30);
        panel.add(result);

        count = new JButton("COUNT");
        count.setBounds(10,60,100,30);
        panel.add(count);

        end = new JButton("END");
        end.setBounds(130,60,100,30);
        panel.add(end);   

        count.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {            
               score = score + 5;
               result.setText(""+score);
           }
        }); 

        end.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {            
               con();
           }
        });       
    }

    public void con() {
        try{
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost/res", "root", "");
            Statement stmt = (Statement) con.createStatement();           
            String insert = "INSERT INTO wyniki VALUES ('" + score + "', '" + name + "')";            
            stmt.executeUpdate(insert);

        }catch (Exception e) {
            System.out.println(e);           
        }
    }

And this is my html code:

<applet code = 'Nowy.class' 
    archive = 'Nowy.jar mysql-connector-java-5.1.27-bin.jar'
    width = 300
    height = 300>
    <param name="permissions" value="sandbox" />
</applet>

I have no idea if it's wrong path in html or other?

Try this.

 <script src="//www.java.com/js/deployJava.js"></script>
    And this to <body> section:

    <script>
        var attributes = {codebase: 'http://my.url/my/path/to/codebase',
                          code: 'my.main.Applet.class',
                          archive: 'my-archive.jar',
                          width: '800', 
                          height: '600'};
        var parameters = {java_arguments: '-Xmx256m'}; // customize per your needs
        var version = '1.5'; // JDK version
        deployJava.runApplet(attributes, parameters, version);
    </script>

If you want to run your Applet into Browser than you must have to signed your JAR file first. Here is the reference for more details:

http://www-personal.umich.edu/~lsiden/tutorials/signed-applet/signed-applet.html

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