你好我是Applets的新手,我需要帮助来理解一些东西。
所以我在eclipse中创建了一个applet,它在单击applet上的按钮时调用另一个类文件及其方法。
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运行它时,这是有效的。我将类文件与applet文件一起导出到jar(Calc.jar)中。然后,我使用以下简单的html来调用applet。这个html是一个本地文件。
<HTML>
<HEAD>
</HEAD>
<BODY>
<div >
<APPLET CODE="mainApplet.class" archive="Calc.jar" WIDTH="800" HEIGHT="500">
</APPLET>
</div>
</BODY>
</HTML>
applet位于MainApplet类中,它调用Function类。
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;
}
}
当在浏览器中启动applet并选中“启动浏览器”复选框并单击该按钮时,应打开一个firefox窗口并启动google.com。在日食中,这完全发生了。但是当applet嵌入在本地站点中时,如果选中“启动浏览器”复选框并单击该按钮,则不会发生任何事情。我有另一个在applet(添加)上使用的函数,它按设计工作。用户可以在两个Jtextfields中输入两个数字,并选中“add”复选框,单击该按钮时,结果将显示在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
提前感谢您的帮助。
答案 0 :(得分:0)
在网页上运行时Java Console中报告了什么?我希望它会显示与安全相关的内容 - 例如
AccessControlException
。我在控制台中收到以下错误:
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError:
更改:
archive="Calc.jar"
到:
archive="Calc.jar,x.jar"
..其中x.jar
是包含WebDriver
类的Jar的名称。