我正在尝试在另一个程序中使用Java组件。该程序是在Tomcat上运行的Web应用程序,我的组件使用Swing打开Web浏览器。提示用户导航到特定页面,并使用JFrame上的控件,页面以HTML格式本地保存。此本地保存的文件稍后将用于进一步详细说明。 问题是,从Eclipse执行时,我的代码没有任何缺陷。但是,当我在Tomcat上运行的应用程序中使用该组件时,它无法正常运行。出现窗口,导航正在运行,数据检索没有问题。问题是主应用程序(在Tomcat上运行的应用程序)必须在组件(Java)完成其工作后恢复执行。显然,出现问题,因为网络应用程序只是挂起,而且永远不会继续。
在我的组件中,我使用两个类:浏览器和帮助器。浏览器基于DJ NativeSwing库,包含初始化和显示简单Web浏览器的逻辑。 Helper类为我提供了一个“前端”方法,允许我在自己的线程中执行浏览器。我使用SwingUtilities.invokeAndWait而不是invokeLater,因为我不太清楚如何使用wait()和notify()。我尝试过但失败了。
总结代码,它看起来像这样:
import javax.swing.SwingUtilities;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
public class Helper {
public String Browse() {
Browser vb = new Browser();
try {
Browser.log("This is Helper, now initiating.");
NativeInterface.open();
Thread th = new Thread(vb);
Browser.log("Helper is invoking thread...");
SwingUtilities.invokeAndWait(th);
NativeInterface.runEventPump();
Browser.log("Thread exited! Back to Helper!");
System.out.println(vb.getData());
Browser.log("Returning data to caller: \n" + vb.getData());
return vb.getData();
}catch (Exception ex) {
ex.printStackTrace();
Browser.log("MainFail: " + ex.getMessage());
return ex.getMessage();
}
}
浏览器类:
import java.awt.BorderLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import chrriis.dj.nativeswing.NativeSwing;
import chrriis.dj.nativeswing.swtimpl.NativeInterface;
import chrriis.dj.nativeswing.swtimpl.components.JWebBrowser;
public class Browser extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
private JButton btn;
private JWebBrowser webBrowser;
private JPanel panel;
private String data = "";
public static void log(String s) {
try {
BufferedWriter ostr = new BufferedWriter(new FileWriter("F:/java_out.log", true));
Date dt = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");
String str = formatter.format(dt) + "\t -> " + s + "\n";
ostr.write(str);
ostr.close();
} catch(Exception ex) {
System.out.println("Logger: " + ex.getMessage());
}
}
public Browser() {
Browser.log("Initializing...");
NativeSwing.initialize();
NativeInterface.open();
panel = new JPanel();
btn = new JButton();
btn.setText("Save Page");
btn.addActionListener(this);
btn.setMargin(new Insets(5, 5, 5, 5));
VisureBrowser.log("Button is ready!");
JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Navigate and save"));
webBrowser = new JWebBrowser();
webBrowser.setButtonBarVisible(true);
webBrowser.setMenuBarVisible(false);
webBrowser.setLocationBarVisible(true);
VisureBrowser.log("Panel is ready!");
this.setTitle("Simple Browser");
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setSize(800, 600);
this.setLayout(new BorderLayout());
this.setLocationByPlatform(true);
this.setLocationRelativeTo(null);
VisureBrowser.log("Frame is ready!");
this.add(btn, BorderLayout.BEFORE_FIRST_LINE);
btn.setVisible(true);
webBrowserPanel.add(webBrowser, BorderLayout.CENTER);
webBrowser.setVisible(true);
this.add(webBrowserPanel, BorderLayout.CENTER);
panel.setVisible(true);
this.setVisible(true);
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
SwingUtilities.updateComponentTreeUI(this);
} catch (Exception e) {
Browser.log("Constructor: " + e.getMessage());
Browser.log(e.getStackTrace().toString());
e.printStackTrace();
}
Browser.log("Look and Feel ready as well! Off to work...");
}
public String getData() {
return this.data;
}
public void close() {
WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
}
public void run() {
Browser.log("Navigating to the index page...");
webBrowser.navigate("http://www.google.com/");
Browser.log("Navigation successful!");
}
@Override
public void actionPerformed(ActionEvent e) {
Browser.log("An action has been performed!");
Object src = e.getSource();
if (src.equals(btn)) {
try {
Browser.log("Getting data...");
this.data = webBrowser.getHTMLContent();
Browser.log("Closing...");
this.close();
} catch( Exception ex) {
Browser.log("ClickFail: " + ex.getMessage());
Browser.log(ex.getStackTrace().toString());
}
}
}
}
我得到的输出来自Eclipse:
12:28:54 -> Initializing...
12:28:55 -> Button is ready!
12:28:55 -> Panel is ready!
12:28:55 -> Frame is ready!
12:28:56 -> Look and Feel ready as well! Off to work...
12:28:56 -> This is Helper, now initiating.
12:28:56 -> Helper is invoking thread...
12:28:56 -> Navigating to the index page...
12:28:56 -> Navigation successful!
12:29:04 -> An action has been performed!
12:29:04 -> Getting data...
12:29:04 -> Closing...
12:29:06 -> Thread exited! Back to Helper!
12:29:06 -> Returning data to caller:
.....here comes all the data.....
以及在Tomcat中执行时获得的数据如下:
12:28:54 -> Initializing...
12:28:55 -> Button is ready!
12:28:55 -> Panel is ready!
12:28:55 -> Frame is ready!
12:28:56 -> Look and Feel ready as well! Off to work...
12:28:56 -> This is Helper, now initiating.
12:28:56 -> Helper is invoking thread...
12:28:56 -> Navigating to the index page...
12:28:56 -> Navigation successful!
12:29:04 -> An action has been performed!
12:29:04 -> Getting data...
12:29:04 -> Closing...
是的,我使用相同的行,但输出是这样的,它只会到结束行。 我没想到的是窗口关闭和主要活动恢复之间的延迟:每次运行大约2-3秒。我觉得它与辅助(GUI)线程没有正确退出有关,但我不知道如何继续。
任何帮助将不胜感激! :)
答案 0 :(得分:0)
你做错了。
Tomcat是一个Web应用程序服务器,不应该弹出任何Swing组件。如果服务器在其他地方运行,则每个用户最终都会在服务器上启动Swing窗口,用户将永远不会看到它们。您正在经历的事情(Tomcat启动Swing窗口并能够实际看到它)只是巧合,因为您的服务器正在localhost
上运行。
您要做的是让您的用户从网络浏览器开始,然后导航到正确的网站以使用您的内容。也就是说,网络浏览器应该已经可用:您不应该在用户上推出一个浏览器。代表。