gpt4 book ai didi

Maven 中的 Javafx TableView : Incompatible types

转载 作者:行者123 更新时间:2023-11-30 02:42:32 33 4
gpt4 key购买 nike

当我运行以下命令时,我在 NetBean 8.2 中使用 maven javafx(运行 JDK 8 时的默认值)来创建表

   @FXML
private TableColumn<NewClass, String> colSTT;

@Override
public void initialize(URL url, ResourceBundle rb) {
colSTT.setCellFactory(TextFieldTableCell.forTableColumn());
}

和 FXML

<TableView xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="imperfect.FXMLController">
<columns>
<TableColumn fx:id="colSTT" prefWidth="75.0" text="C1" />
</columns>
</TableView>

还有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>imperfect</groupId>
<artifactId>Imperfect</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>Imperfect</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mainClass>imperfect.MainApp</mainClass>
</properties>

<organization>
<!-- Used as the 'Vendor' for JNLP generation -->
<name>Your Organisation</name>
</organization>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<excludeScope>system</excludeScope>
<excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>unpack-dependencies</id>

<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${java.home}/../bin/javafxpackager</executable>
<arguments>
<argument>-createjar</argument>
<argument>-nocss2bin</argument>
<argument>-appclass</argument>
<argument>${mainClass}</argument>
<argument>-srcdir</argument>
<argument>${project.build.directory}/classes</argument>
<argument>-outdir</argument>
<argument>${project.build.directory}</argument>
<argument>-outfile</argument>
<argument>${project.build.finalName}.jar</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>default-cli</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${java.home}/bin/java</executable>
<commandlineArgs>${runfx.args}</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>${java.home}/lib/jfxrt.jar</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/test/java</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</build>
</project>

总是出错如下

COMPILATION ERROR : 
-------------------------------------------------------------
imperfect/FXMLController.java:[28,64] incompatible types: javafx.util.Callback<javafx.scene.control.TableColumn<java.lang.Object,java.lang.String>,javafx.scene.control.TableCell<java.lang.Object,java.lang.String>> cannot be converted to javafx.util.Callback<javafx.scene.control.TableColumn<imperfect.NewClass,java.lang.String>,javafx.scene.control.TableCell<imperfect.NewClass,java.lang.String>>
1 error

如果我在javafx(不是maven)上运行相同的命令成功。请帮我修复它!

最佳答案

您的 Maven 配置设置为将代码编译为 Java 7(或 1.7)。正如 James_D 所说,这意味着 Java(或更准确地说,java 编译器 javac)无法正确推断回调的类型。

更改配置以编译为 Java 8 (1.8) 应该可以解决您的问题:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArguments>
<bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
</compilerArguments>
</configuration>
</plugin>

另请参阅the documentation用于 Maven 编译器插件。

如果由于某种原因您需要在 Java 7 中编译代码(为了向后兼容),您可以使用 James_D 提出的显式设置类型的解决方案

colSTT.setCellFactory(TextFieldTableCell.<NewClass>forTableC‌​olumn()); 

但除非您确定需要与 Java 7 向后兼容,否则最好在 Java 8 中编译代码。

关于Maven 中的 Javafx TableView : Incompatible types,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41219017/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com