我已经使用Netbeans中的JUNG库创建了一个JApplet,它可以编译并正常运行。但是,当我尝试创建运行该applet的html文件时,仅显示灰色窗格,但缺少组件。
我的课是:

   public class View extends JApplet {

   //Here I declare the buttons etc..

   public View()
{
    initializeComponent();
            fetchGraphs();

}


   public static void main(String[] args) throws IOException{

    f = new JFrame();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    x = screenSize.width;
    y = screenSize.height;


    f.getContentPane().add(new View());
    f.setTitle("Social Network Privacy Settings and Access Control");
    f.setLocation(new Point(15, 20));
    f.setSize(new Dimension(x-20,y-50));
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setResizable(false);

    f.setVisible(true);
}
}


方法initializeComponent()将所有组件添加到主窗口。我使用JFrameBuilder构建了一些基本组件。 JFrameBuilder使用方法addComponent(container,component,x,y,width,height)添加组件

我使用下面的代码:

  contentPane = (JPanel)this.getContentPane();

  //to create the japplet contentpane

  addComponent(contentPane, genGraphButton, (int)(0.35*x),(int)(0.63*y),
  (int)(0.2*x),28);

  // to add components


然后我创建一个html文件:

    <applet code = 'MyPackage.View'
    archive = 'MyProject.jar',
    width = 1600,
    height = 800/>


在/ dist文件夹中,但是当我尝试使用Mozilla Firefox打开它时,只会显示一个灰色窗格。奇怪的是,这次我使用netbeans JBuilder创建了另一个简单的applet,它可以在网页中正常运行。

我真的需要一些帮助!

最佳答案

您提到了JUNG库,它依赖于两个第三方库,即Collections-Generic和Cern Colt科学库1.2.0。如@othman所述,它们需要添加到applet的运行时类路径中(添加到archive元素的applet属性中)。

但是,为了使我们很清楚,请确保HTML不仅仅包含applet元素。像这样:

<html>
<body>
<applet
    code='MyPackage.View'
    archive='MyProject.jar,jung.jar,collections.jar,colt-scientific.jar'
    alt='Java is DISABLED in this browser!'
    width='1600'
    height='800'>
This browser does not recognize the applet element!
</applet>
</body>
</html>


当然,您需要将最后3个Jar的名称更改为其真实名称。

10-05 20:59
查看更多