【发布时间】:2023-03-29 18:06:01
【问题描述】:
我想在 InternalFrame 中有一个可调整大小的 webview,但是我崩溃了。这是我的代码:
/**
* Create the frame.
*/
public TestBtn() {
setClosable(true);
setResizable(true);
setMaximizable(true);
setIconifiable(true);
getContentPane().setBackground(Color.GRAY);
JFXPanel panel = new JFXPanel();
getContentPane().add(panel, BorderLayout.CENTER);
getContentPane().add(panel, BorderLayout.CENTER);
setBounds(100, 100, 764, 546);
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX(panel);
}
});
this.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
initFX(panel);
}
});
}
private static void initFX(JFXPanel fxPanel) {
// This method is invoked on the JavaFX thread
Scene scene = createScene();
fxPanel.setScene(scene);
}
private static Scene createScene() {
Group root = new Group();
Scene scene = new Scene(root);
WebView view = new WebView();
WebEngine engine = view.getEngine();
engine.onResizedProperty();
engine.load("http://google.bg");
root.getChildren().add(view);
return (scene);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
TestBtn frame = new TestBtn();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
尝试最大化 InternalFrame 窗口后出现此错误:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source)
at javafx.scene.web.WebEngine.checkThread(Unknown Source)
at javafx.scene.web.WebEngine.<init>(Unknown Source)
at javafx.scene.web.WebEngine.<init>(Unknown Source)
at javafx.scene.web.WebView.<init>(Unknown Source)
at test.TestBtn.createScene(TestBtn.java:79)
at test.TestBtn.initFX(TestBtn.java:71)
at test.TestBtn.access$0(TestBtn.java:69)
at test.TestBtn$2$1.componentResized(TestBtn.java:63)
at java.awt.Component.processComponentEvent(Unknown Source)
at javafx.embed.swing.JFXPanel.processComponentEvent(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.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
【问题讨论】:
-
您将同一个面板两次添加到框架中。
-
CodeRunner 我不明白你的意思我在 java 中添加了两次面板我是新的 ^^:D
-
在第 11 行和第 12 行,您将在同一个地方两次添加同一个面板。
-
我修复了这个但又出现了这个错误
-
请贴出全班代码。
标签: java swing javafx jfxpanel