您好,我是 Applets 的新手,我需要帮助来理解某些东西。
因此,我在 Eclipse 中创建了一个小程序,当单击小程序上的按钮时,它会调用另一个类文件及其方法。
public class mainApplet extends JApplet {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private int x;
private int y;
private int result;
public mainApplet() {
textField = new JTextField();
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setColumns(10);
JButton btnCalculate = new JButton("Calculate");
final JCheckBox ChckbxADD= new JCheckBox("Add");
final JCheckBox chckbxLaunch = new JCheckBox("Launch Browser");
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(49)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(26)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createSequentialGroup()
.addGap(89)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(btnCalculate)
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(ChckbxADD)
.addComponent(chckbxLaunch)))))
.addContainerGap(203, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(48)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(textField_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addComponent(ChckbxADD)
.addGap(3)
.addComponent(chckbxLaunch)
.addGap(20)
.addComponent(btnCalculate)
.addGap(28)
.addComponent(textField_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(74, Short.MAX_VALUE))
);
getContentPane().setLayout(groupLayout);
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Functions func = new Functions();
if (ChckbxADD.isSelected()){
x=Integer.parseInt(textField.getText());
y=Integer.parseInt(textField_1.getText());
result=func.addition(x, y);
textField_2.setText(String.valueOf(result));
}
if(chckbxLaunch.isSelected()){
func.Launch();
}
}
});
}
}
这在我通过 Eclipse 运行时有效。我将类文件与小程序文件一起导出到 jar (Calc.jar) 中。然后,我使用以下简单的 html 来调用小程序。这个 html 是一个本地文件。
<HTML>
<HEAD>
</HEAD>
<BODY>
<div >
<APPLET CODE="mainApplet.class" archive="Calc.jar" WIDTH="800" HEIGHT="500">
</APPLET>
</div>
</BODY>
</HTML>
小程序在 MainApplet 类中,它调用 Functions 类。
public class Functions {
public void Launch(){
WebDriver driver= new FirefoxDriver();
driver.get("http://www.google.com");
}
public int addition(int x, int y){
int result;
result=x + y;
return result;
}
}
在浏览器中启动小程序并选中“启动浏览器”复选框并单击按钮时,应打开一个 firefox 窗口并启动 google.com。在日食中,这完美地发生了。但是当小程序嵌入到本地站点时,选中“启动浏览器”复选框并单击按钮时不会发生任何事情。我有另一个用于小程序(加法)的函数,它按设计工作。用户可以在两个 Jtextfields 中输入两个数字,并选中“添加”复选框,当单击按钮时,结果将显示在 applet 的另一个 jtextfield 上。这工作正常,但其他功能不起作用。
更新:
以下是控制台中的异常:
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
at mainApplet$1.actionPerformed(mainApplet.java:75)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 36 more
提前感谢您的帮助。