【发布时间】:2021-05-03 23:13:43
【问题描述】:
我们使用 JavaFX WebView 作为我们的应用内浏览器。它在 Windows 10 下运行良好。
但是,当我们在 macOS Catalina 10.15.7 下运行时,所有显示的毛线都消失了。该应用在 Java 8 上运行。
这是我们简单的应用内浏览器的代码。
SimpleSwingBrowser.java
public class SimpleSwingBrowser extends JDialog {
private final JFXPanel jfxPanel = new JFXPanel();
private WebEngine engine;
private String loadedURL = null;
private final JPanel panel = new JPanel(new BorderLayout());
public SimpleSwingBrowser() {
super(JStock.instance(), JDialog.ModalityType.APPLICATION_MODAL);
initComponents();
}
private void initComponents() {
createScene();
// http://stackoverflow.com/questions/11269632/javafx-hmtleditor-doesnt-react-on-return-key
jfxPanel.addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 10) {
e.setKeyChar((char) 13);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(e);
}
}
});
panel.add(jfxPanel, BorderLayout.CENTER);
getContentPane().add(panel);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-460)/2, (screenSize.height-680)/2, 460, 680);
}
private void createScene() {
Platform.runLater(new Runnable() {
@Override
public void run() {
final WebView view = new WebView();
engine = view.getEngine();
engine.titleProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, final String newValue) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SimpleSwingBrowser.this.setTitle(newValue);
}
});
}
});
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<State>() {
@Override
public void changed(ObservableValue<? extends State> observable, State oldValue, final State newValue) {
if (newValue == FAILED) {
final int result = JOptionPane.showConfirmDialog(
panel,
MessagesBundle.getString("error_message_unable_connect_to_internet"),
MessagesBundle.getString("error_title_unable_connect_to_internet"),
JOptionPane.YES_NO_OPTION);
if (result == JOptionPane.YES_OPTION) {
if (loadedURL != null) {
engine.load(loadedURL);
}
}
}
}
});
// http://stackoverflow.com/questions/11206942/how-to-hide-scrollbars-in-the-javafx-webview
// hide webview scrollbars whenever they appear.
view.getChildrenUnmodifiable().addListener(new ListChangeListener<Node>() {
@Override
public void onChanged(Change<? extends Node> change) {
Set<Node> deadSeaScrolls = view.lookupAll(".scroll-bar");
for (Node scroll : deadSeaScrolls) {
scroll.setVisible(false);
}
}
});
jfxPanel.setScene(new Scene(view));
}
});
}
public void loadURL(final String url) {
Platform.runLater(new Runnable() {
@Override
public void run() {
String tmp = toURL(url);
if (tmp == null) {
tmp = toURL("http://" + url);
}
loadedURL = tmp;
engine.load(tmp);
}
});
}
private static String toURL(String str) {
try {
return new URL(str).toExternalForm();
} catch (MalformedURLException exception) {
return null;
}
}
}
知道为什么会这样吗?有什么我可以解决的吗?谢谢。
【问题讨论】:
-
题外话,但我相信“发丝”实际上是指“乱丝”,是吗?
-
这是否回答了您的问题stackoverflow.com/q/41952734/1133011?这里还声明它已在 java 8u152 bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8088205 中修复