![](/img/trans.png)
[英]What does “Exception in thread ”main“ java.lang.NoClassDefFoundError: javafx/embed/swing/JFXPanel” mean and how do I fix it?
[英]How to fix exception when creating JFXPanel in Swing with JavaFX 12
我有一個帶有基於 Swing 的 UI 的大型 Java 應用程序,它使用 JavaFX 通過 JFXPanel 組件在部分窗口中呈現視頻面板和 WebView 面板。
JDK 8 一切正常,但我正在遷移到 OpenJDK 12 和 JavaFX 12,並且在創建 JFXPanel 時遇到運行時異常。 當我調用“new JFXPanel()”時,出現異常:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalAccessError: class
javafx.embed.swing.JFXPanel (in unnamed module @0x44bebd15) cannot access
class com.sun.javafx.logging.PlatformLogger (in module javafx.base) because
module javafx.base does not export com.sun.javafx.logging to unnamed module
@0x44bebd15
來自 javafx-sdk-12.0.2 的 javafx.base 和 javafx.swing jar 文件包含在我的 Eclipse 項目和運行時中(以及一些其他 JavaFX jar)。
我正在使用 VM 參數:--module-path /Library/Java/javafx-sdk-12.0.2/lib --add-modules javafx.controls,javafx.web,javafx.media,javafx.base
這是 JavaFX 12 中的錯誤還是我做錯了什么?
htmlPanel = new JFXPanel() {
public Dimension getPreferredSize() {
return new Dimension(CoreDrawer.scaledSize(width),
CoreDrawer.scaledSize(height));
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
};
Platform.runLater(new Runnable() { // run on the JavaFX thread
public void run() {
initFX(htmlPanel);
}
});
Platform.setImplicitExit(false);
thePanel.add(htmlPanel);
函數 initFX() 向 JFXPanel 添加了一個場景,但程序沒有通過“new JFXPanel()”
評論 'Use --add-modules javafx.web,javafx.media,javafx.swing' 由 Slaw 為我工作。 需要將 javafx.swing 附加到我的運行配置的“VM 選項”。
public class Main extends Application{
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.initStyle(StageStyle.TRANSPARENT);
Parent root=FXMLLoader.load(Main.class.getResource("../../../com/chatbot/view/chatgui.fxml"));
Scene scene=new Scene(root);
scene.setFill(Color.TRANSPARENT);
primaryStage.setScene(scene);
primaryStage.show();
}
public static synchronized void main(String[] args) throws InterruptedException {
InterfaceGraphiqueAuth fen = new InterfaceGraphiqueAuth();
while(!fen.getFx()){
try{
Thread.currentThread().wait();;
}catch(Exception exc){}
}
launch(args);
}
}
我無法修復異常,所以我使用了這個替代方案。 InterfaceGraphiqueAuth 是一個結束時的 Swing 應用程序,它將我命名為 fx 的布爾值設置為 false,因此 getFx 返回 false 並打開 javafx 窗口。 我添加了wait 方法來等待swing 應用程序將布爾值fx 設置為false。
這就是 InterfaceGraphiqueAuth 中發生的事情:
if((loginTF.getText().compareTo(compte.getUsername()) == 0)&& (passwordTF.getText().compareTo(compte.getCode()) == 0)){
//le compte existe dans la base de donn�es
k=true;
prenom=compte.getPrenom();
lastname=compte.getNom();
sexe=compte.getSexe();
}
}
if(k){
dispose();
fx=true;
}
else JOptionPane.showMessageDialog(null, "Echec");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.