我在javafx应用程序中使用了jxbrowser:
public class Main extends Application {
private Stage primaryStage;
private Browser browser;
private boolean firstMinimize;
private TrayIcon trayIcon;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
firstMinimize = true;
Platform.setImplicitExit(false);
createTrayIcon();
browser = new Browser();
BrowserView browserView = new BrowserView(browser);
StackPane pane = new StackPane();
pane.getChildren().add(browserView);
Scene scene = new Scene(pane, 380, 500);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.setScene(scene);
primaryStage.show();
browser.addRenderListener(new RenderAdapter() {
@Override
public void onRenderGone(RenderEvent event) {
System.out.println("here called...");
Browser browser = event.getBrowser();
// Restore Browser instance by loading the same URL
browser.loadURL(browser.getURL());
}
});
initialize();
// if not logged in
showLogin();
}
public void initialize() {
initCloseButton(primaryStage);
initMinimizeButton(primaryStage);
}
public void initCloseButton(Stage primaryStage) {
browser.registerFunction("Close", new BrowserFunction() {
@Override
public JSValue invoke(JSValue... args) {
Platform.runLater(new Runnable() {
@Override
public void run() {
closeToTray(primaryStage);
}
});
return null;
}
});
}
public void initMinimizeButton(Stage primaryStage) {
browser.registerFunction("Minimize", new BrowserFunction() {
@Override
public JSValue invoke(JSValue... args) {
Platform.runLater(new Runnable() {
@Override
public void run() {
primaryStage.setIconified(true);
}
});
return null;
}
});
}
public void createTrayIcon() {
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
java.awt.Image image = null;
try {
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
image = ImageIO.read(url);
} catch (IOException ex) {
System.out.println(ex);
}
final ActionListener closeListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
System.exit(0);
}
};
final ActionListener showListener = new ActionListener() {
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
Platform.runLater(new Runnable() {
@Override
public void run() {
// primaryStage.setOpacity(0);
primaryStage.show();
}
});
}
};
PopupMenu popup = new PopupMenu();
MenuItem showItem = new MenuItem("Show");
showItem.addActionListener(showListener);
popup.add(showItem);
MenuItem closeItem = new MenuItem("Close");
closeItem.addActionListener(closeListener);
popup.add(closeItem);
trayIcon = new TrayIcon(image, "RezRem", popup);
trayIcon.addActionListener(showListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println(e);
}
}
}
public void showMinimizeMessage() {
if (firstMinimize) {
trayIcon.displayMessage("some message",
"Some other message.",
TrayIcon.MessageType.INFO);
firstMinimize = false;
}
}
private void closeToTray(Stage primaryStage) {
Platform.runLater(new Runnable() {
@Override
public void run() {
if (SystemTray.isSupported()) {
// primaryStage.setOpacity(0);
primaryStage.hide();
showMinimizeMessage();
}
else {
System.exit(0);
}
}
});
}
public void showLogin() {
browser.loadURL(Main.class.getResource("templates/login.html").toExternalForm());
browser.registerFunction("Login", new BrowserFunction() {
@Override
public JSValue invoke(JSValue... args) {
for (JSValue arg : args) {
System.out.println("arg = " + arg.getString());
}
return JSValue.create("Hello!");
}
});
}
}
我在设置功能" closeToTray"时设置了关闭按钮以隐藏舞台。(为了将其最小化到系统托盘)但是然后调用stage.show()会调出浏览器,但浏览器视图是只是一个整体的白色,换句话说,渲染的页面就这样消失了。这是最小化到系统托盘之前和之后的屏幕截图: Before clicking close btn,after clicking show from system tray
任何想法?
提前致谢