Package 'javafx.stage' is declared in module 'javafx.graphics', which is not in the module graph

本文记录了解决IntelliJ IDEA中JavaFX项目出现大量红色错误提示的问题过程。作者发现将language level从10调整至8后,错误提示消失,项目恢复正常。问题源于IDEA与Java版本间的不兼容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用itellij idea写javafx程序时,刚开始挺顺利的,功能也实现了。后来过了一段时间重新打开idea看项目时忽然发现一大片红色错误提示。我以为是少jar包导致的,就导入jar包,专门创建了一个maven工程重构项目来解决jar包问题,然而并没有说什么卵用,还是一大片红色。把光标放到红色代码上就提示如标题所示的错误信息。也是很郁闷。

直接运行一下项目,发现虽然报错,但是项目能正常运行,功能都能实现。很奇特。

通过查资料了解到javafx相关的依赖是java jdk自带的,不需要额外引用任何jar包。那么问题就应该出现在idea本身的某项配置上。

仔细回想之前都做了哪些重要操作可能导致这个错误。逐个查看、修改,看效果怎么样。

错误提示中提到了module这个单词,结合之前的操作,严重怀疑是下图所示的地方配置有误,当查看到如下图所示的地方时奇迹出现了:

当初使用lambda表达式的时候因为默认language level是5 ,不支持lambda表达式,所以把它改成了10(我中的jdk版本是10),本以为越高越好,正好配套了,并且当时该了之后没注意到报错,直接运行项目功能可以实现就直接完事了。

现在把language level从10改成8(从8起,开始支持lambda表达式),奇迹出现了,一大片红色报警突然全部消失,哈哈,好高兴!

至于为什么非8不可,那就不得而知了,idea自身的特殊配置,也不好深究。问题完美解决就行了,别的管他呢。

 

总结:这个错误是某些东西不匹配造成的,结合报错信息,加上一点灵感,问题得以解决。

 

The error message you're referring to is related to JavaFX and the use of the `Stage` class. In JavaFX (part of the JavaFX SDK), the `Stage` class is typically used for creating and managing user interfaces with a modal window. When working with JavaFX, it's important to include the necessary import statements and have proper dependencies set. The "may not be accessible to clients due to missing 'requires transitive'" warning suggests that the code attempting to use `Stage` doesn't have a `javafx.graphics` or a suitable transitive dependency declared in its build file (e.g., Maven's `pom.xml` or Gradle's `build.gradle`). This means that the compiler cannot automatically resolve the required classes because the required level of transitivity is not enabled by default. To fix this issue: 1. Make sure your project has the appropriate `import javafx.stage.Stage;` statement. 2. Check if you've included the JavaFX dependencies in your build tool configuration. For example, in Maven, add the following to your `pom.xml`: ```xml <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>${javafx.version}</version> <transitive>true</transitive> </dependency> ``` For Gradle, it might look like: ```groovy implementation 'com.oracle:javafx.controls:16+' ``` 3. If you're using a modular build system (Java 9+), ensure that the required modules (`javafx.graphics`, etc.) are explicitly declared. Remember to replace `${javafx.version}` with the actual version of JavaFX you're using.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值