public class Applet1 extends Applet { boolean isStandalone = false; PopupMenu popupMenu1 = new PopupMenu(); MenuItem menuItem1 = new MenuItem(); MenuItem menuItem2 = new MenuItem(); MenuItem menuItem3 = new MenuItem(); Button button1 = new Button(); XYLayout xYLayout1 = new XYLayout();/*面板改变为XYLayout布局,使用的是非jdk标准类*/ Button button2 = new Button(); Button button3 = new Button();/*添加三个按钮*/ public void init() { menuItem1.setLabel("1"); menuItem2.setLabel("2"); menuItem3.setLabel("3"); button1.setLabel("button1"); button1.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePRessed(MouseEvent e) { button1_mousePressed(e); } }); this.setLayout(xYLayout1); button2.setLabel("button2"); button2.addMouseListener(new java.awt.event.MouseAdapter() { public void mousePressed(MouseEvent e) { button1_mousePressed(e); } }); button3.setLabel("button3"); button3.addMouseListener(new java.awt.event.MouseAdapter() {/*三个按钮的右键事件都为一个*/ public void mousePressed(MouseEvent e) { button1_mousePressed(e); } }); popupMenu1.add(menuItem1); popupMenu1.add(menuItem2); popupMenu1.add(menuItem3); this.add(button1, new XYConstraints(155, 5, -1, -1)); add(popupMenu1); this.add(button2, new XYConstraints(49, 94, 101, 32)); this.add(button3, new XYConstraints(235, 92, 98, 36)); } void button1_mousePressed(MouseEvent e) { int mods=e.getModifiers(); if((mods&InputEvent.BUTTON3_MASK)!=0) { popupMenu1.show(button1,e.getX(),e.getY()); } } }
通过在jbuilder的预览中可以看到,三个按钮未改动位置,而且右键单击也按要求在一个按钮上出现。很可惜的是这样的applet无法发布出来,我们试着打开class目录下的Applet1.Html,IE报出一个出错信息:"load:test1.Applet1 cant't be instantiated"。原因就是IE并不熟悉com.borland.jbcl.layout下面的类,而这里使用了XYLayout。.那么我们开始制作包含该XYLayout类的JAR来发布该applet。
还是通过new一个 Archive Builder开始,
Archive type : Applet
Project classes and resources : Include required classes and known resources
Required classes and resources : add test1.Applet1.class
再点击next,就到了要害的一步,显示的JBCL和DataEXPress都是applet所需要调用到的类,而且都是非jdk标准的,可以通过在这里选择后也包含到这个JAR文件中,供applet调用,默认的设定是Include required classes and all resources,即把applet所需要调用的类包含到JAR中,并且还包括这个类所中所包含的所有资源,而不管applet是否会用到。所以我们一般情况下只需要选择include required classes and known resources,资源文件要尽量少的包含到JAR中,以避免JAR文件过于庞大。记住是要让JBCL和DataExpress都选择为Include Deps。这样便很轻松的完成了相关类的添加工作。