不能同时将 miglayout 与 swing 和 javafx 一起使用

Cannot use miglayout with swing and javafx at the same time

我的应用程序需要作为 javafx 应用程序启动并创建一些 swing jframe 实例。我想将 MigLayout 用于 java swing 容器和 javafx 窗格。只要我仅在 javafx 中使用 miglayout,该应用程序就可以正常工作。当我也将它用于摆动时,出现此错误:

java.lang.AbstractMethodError: Receiver class net.miginfocom.swing.SwingComponentWrapper does not define or inherit an implementation of the resolved method abstract getContentBias()I of interface net.miginfocom.layout.ComponentWrapper.

这是我的代码:

package com.myproj.mavenproject13;

import java.awt.Dimension;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javax.swing.JFrame;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
import org.tbee.javafx.scene.layout.MigPane;


public class HelloWorld extends Application {
    private MigLayout migLayout = new MigLayout("debug, fillx", "", "");
    
    @Override
    public void start(Stage primaryStage) {
        MigPane migPane = new MigPane("debug, fillx", "", "");
        migPane.add(getButton1(), "grow, push, wrap");
        migPane.add(getButton2(), "grow, push");
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(new Scene(migPane, 300, 250));
        primaryStage.show();
    }
    
    private Button getButton1(){
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new javafx.event.EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                System.out.println("asd");
            }
        });
        return btn;
    }
    
    private Button getButton2(){
        Button btn = new Button();
        btn.setText("Create frame");
        btn.setOnAction(new javafx.event.EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                JFrame f = new JFrame();
                f.setLayout(migLayout);
                JTextField jtf = new JTextField("asd");
                f.add(jtf, "growx, pushx");
                f.setTitle("my frame");
                f.setPreferredSize(new Dimension(400, 400));
                f.setVisible(true);
            }
        });
        return btn;
    }
}

主要class是:

package com.proj.mavenproject13;

import javafx.application.Application;

public class Main {

    public static void main(String[] args) {
        Application.launch(HelloWorld.class, args);
    }

}

这是我项目的 pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.proj</groupId>
    <artifactId>mavenproject13</artifactId>
    <version>1.0.0-RELEASE</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>17.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout-javafx</artifactId>
            <version>11.0</version>
        </dependency>
        <dependency>
            <groupId>com.miglayout</groupId>
            <artifactId>miglayout</artifactId>
            <version>3.7.4</version>
        </dependency>
    </dependencies>
</project>

我想您至少必须为 Swing 和 JavaFX 使用相同的版本,否则会发生冲突,因为这两个实现共享一些代码。您也应该使用最新版本,所以我认为这应该没问题。

<dependency>
    <groupId>com.miglayout</groupId>
    <artifactId>miglayout-javafx</artifactId>
    <version>11.0</version>
</dependency>
<dependency>
    <groupId>com.miglayout</groupId>
    <artifactId>miglayout-swing</artifactId>
    <version>11.0</version>
</dependency>