我正在尝试在我的 JavaFX 应用程序中禁用 Windows 显示缩放(以及在其他操作系统中)。-Dprism.allowhidpi="false"
在 IntelliJ 运行配置 VM 选项中设置有效,但在代码中设置系统属性无效。我想在代码中设置它,这样它就可以在任何 JVM 设置中工作,比如 GraalVM / Gluon Substrate。
这是 JavaFX 中的错误还是在代码中如何使用?以下示例不起作用,如果在 Windows 中设置缩放,则舞台被缩放:
public class Main extends Application {
public static void main(String[] args) {
System.setProperty("prism.allowhidpi", "false");
launch(args);
}
@Override
public void start(Stage stage) {
stage.setWidth(1280);
stage.setHeight(720);
Scene scene = new Scene(new Label("SCALE TEST"));
stage.setScene(scene);
stage.show();
}
}
编辑:如评论中所述,创建一个单独的启动器类来设置属性,然后调用 Application 类。但是,它似乎不适用于 Gluon Substrate。这是我项目的相关部分。我还添加了另外两个 prism 属性,以确保新的主类在 Substrate 中正确加载,并且另外两个属性确实有效。如果我运行mvn javafx:run
缩放关闭,如果我运行mvn client:build
并且mvn client:run
缩放打开但其他一切都是一样的。
public class Main {
public static void main(String[] args) {
System.setProperty("prism.allowhidpi", "false"); // doesn't work with Substrate
System.setProperty("prism.lcdtext", "false"); // works
System.setProperty("prism.subpixeltext", "false"); // works
MainFXML.run(args);
}
}
应用程序
public class MainFXML extends Application {
@Override
public void start(Stage stage) throws Exception {
String fxmlFile = "/fxml/FXMLDocument.fxml";
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlFile));
Parent root = loader.load();
root.getStylesheets().add(getClass().getResource("/styles/Style.css").toString());
FXMLDocumentController gui = (FXMLDocumentController) loader.getController();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
stage.setResizable(false);
// create logic class etc
}
public static void run(String[] args) {
launch(args);
}
}
绒球
<?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>ank</groupId>
<artifactId>PROJ</artifactId>
<name>PROJ</name>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>PROJ</finalName>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.3</version>
<configuration>
<mainClass>ank.fxml.Main</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>com.gluonhq</groupId>
<artifactId>client-maven-plugin</artifactId>
<version>0.1.38</version>
<configuration>
<mainClass>ank.fxml.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>15</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>15</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>
我还尝试将 pom 文件中的属性设置为 nativeImageArgs,但它们都不起作用。
<plugin>
<groupId>com.gluonhq</groupId>
<artifactId>client-maven-plugin</artifactId>
<version>0.1.38</version>
<configuration>
<mainClass>ank.fxml.Main</mainClass>
<nativeImageArgs>
<arg>-Dprism.allowhidpi=false</arg>
<arg>-Dprism.lcdtext=false</arg>
<arg>-Dprism.subpixeltext=false</arg>
</nativeImageArgs>
</configuration>
</plugin>