HTML显示小程序无法正常工作

时间:2013-12-11 03:03:36

标签: html swing applet

我想在桌面上使用简单的HTML页面测试一些Java Applet。下面是我为该类使用而编写的代码,它没有显示applet。找不到错误类。

HTML:

<html>
<head>
<title>Java HTML Example</title>

<body>
This is a test of the JApplet
<br>
<embed Code="LifeCycle.class" width=300 height=300>
</embed>
</body>
</html>

Java代码:

为什么这适用于Applet而不适用于JApplet ????

import java.awt.*;
import java.applet.Applet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * This class can be run as either a JApplet or a JFrame
 * @author home-1
 */
public class LifeCycle extends Applet
{

    private static final long serialVersionUID = 1L;
    String output = "test";
    String event;

    //All of the GUI for the Applet goes here in the initialize
    public void init()
    {
         setLayout(new BorderLayout()); 
         Button okButton = new Button("A button"); 
         add(new JButton("North"),BorderLayout.NORTH);
         add(new JButton("South"),BorderLayout.SOUTH);
         add(new JButton("East"),BorderLayout.EAST);
         setBackground(Color.RED);
//         TextField nameField = new TextField("A TextField",100); 
//         CheckboxGroup  radioGroup = new CheckboxGroup(); 
//         Checkbox   radio1 = new Checkbox("Radio1", radioGroup,false); 
//         Checkbox   radio2 = new Checkbox("Radio2", radioGroup,true); 
//         Checkbox   option = new Checkbox("Option",false); 
//
//         okButton.setBounds(20,20,100,30); 
//         nameField.setBounds(20,70,100,40); 
//         radio1.setBounds(20,120,100,30); 
//         radio2.setBounds(140,120,100,30); 
//         option.setBounds(20,170,100,30); 
//   
//         add(okButton); 
//       add(nameField); 
//       add(radio1); 
//       add(radio2); 
//       add(option); 

         event = "\nInitializing...";
         printOutput();
    }

    public void start()
    {
        event = "\nStarting..."; 
        printOutput();
    }

    public void stop()
    {
        event = "\nStopping...";
        printOutput();
    }

    public void destroy()
    {
        event = "\nDestroying...";
        printOutput();
    }

    private void printOutput()
    {
        System.out.println(event);
        output += event;
      //  repaint();
    }

    private void gui() {
        JFrame f = new JFrame("Not resizable");
        JPanel d = new JPanel();

        d.setLayout(new BorderLayout());
        d.add(new JButton("top    button"),BorderLayout.NORTH);
        d.add(new JButton("bottom button"),BorderLayout.SOUTH);
        d.add(new JButton("center button"),BorderLayout.EAST);
        d.setBackground(Color.RED);

        f.add(d);
        f.setSize(545,340); 
        f.setResizable(false);
        f.setLocationRelativeTo(null);
        f.setTitle("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
    }

    public void paint(Graphics g)
    {
        System.out.println("Graphics Paint Method!");
        g.drawString(output, 100, 100);
        g.drawString("Hello", 10, 10);
    }

    public static void main(String[] args) {
        LifeCycle l = new LifeCycle();
        l.gui();
    }
}

1 个答案:

答案 0 :(得分:1)

也许您可以尝试以下

   <embed id="testapplet"
   type="application/x-java-applet;version=1.6"
   width="256" height="256" 
   archive="mytest.jar"
   code="LifeCycle.class"
   pluginspage="http://java.com/download/"
   myParam="My Param Value" />

进一步阅读here