我有一个 JApplet,它在“init”期间启动 JFrame。该JFrame中有一些操作,用户最终关闭了JFrame。
现在我想通过 javascript 通知浏览器有什么事情要做。
如何做到这一点?如果只是小程序的话,下面这个简单的功能就足够了
public void notifyBrowser() {
JSObject browserWindow = JSObject.getWindow(this);
browserWindow.eval("try{RefreshFilesInLongue();}catch(e){alert('error');}");
}
但是代码中的“this”指的是小程序对象,当我在 JFrame 中时,我不知道如何访问该对象。
为了获得焦点,JFrame 被调用如下:
public void init() {
paramPostUrl = this.getParameter("postUrl");
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
ScanFrame scanFrame = new ScanFrame();
scanFrame.setPostUrl(paramPostUrl);
scanFrame.setVisible(true);
try {
Thread.sleep(300L);
scanFrame.toFront();
} catch (InterruptedException ex) {
Logger.getLogger(Applet.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}
所以我的问题是:
1. 如何在 scanFrame 对象中调用 notifyBrowser()
2. 或者如何检测 scanFrame 已关闭并在 applet 对象中成功调用 notifyBrowser?
谢谢你的帮助。
所以修改后的代码是这样的:
public class Applet extends javax.swing.JApplet {
...
public void init() {
paramPostUrl = this.getParameter("postUrl");
myApplet = this;
/* Create and display the applet */
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
initComponents();
ScanFrame scanFrame = new ScanFrame(myApplet);
...
和扫描帧
public class ScanFrame extends javax.swing.JFrame implements ScannerListener {
...
private JApplet appletObj;
public ScanFrame(JApplet myApplet) {
appletObj = myApplet;
....
private void sendScannedDocumentToCallingApp() {
....
JSObject browserWindow;
browserWindow = JSObject.getWindow((JApplet)appletObj);
browserWindow.eval("try{RefreshFilesInLongue();}catch(e){alert('error');}");
}
和我得到的错误:
Exception in thread "AWT-EventQueue-2" java.util.NoSuchElementException
at java.util.LinkedList.getFirst(Unknown Source)
at java.awt.SequencedEvent.getFirst(Unknown Source)
at java.awt.SequencedEvent.dispatch(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)