问题描述
我正在尝试在我的 JavaFX 应用程序中禁用 Windows 显示缩放(以及在其他操作系统中)。在 IntelliJ 运行配置 VM 选项中设置 -Dprism.allowhidpi="false"
有效,但在代码中设置系统属性无效。我想在代码中设置它,以便它可以在任何 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。这是我的项目的相关部分。我还添加了另外两个棱镜属性,以确保新的主类在 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>
解决方法
经过多次搜索,我设法让它工作,至少在 Windows 上是这样。而不是使用 prism.allowhidpi
标志,将 glass.win.uiScale
设置为 100% 可以忽略 Windows 缩放。这可以在代码中设置它并将其作为参数传递。
public class Main {
public static void main(String[] args) {
System.setProperty("glass.win.uiScale","100%"); // instead of allowhidpi
MainFXML.run(args);
}
}
请注意,将 prism.allowhidpi
设置为 false 将否定我测试的缩放覆盖。
似乎你想在 GUI 启动之前为你的应用设置系统环境,有几种方法可以做到:
1.使用脚本启动您的应用
#sh or cmd
#set system environment
#java -jar your_app_jar
2.使用原生加载器
loader[set system environment]->your app
3.使用带有 ProcessBuilder 的 java 加载器来启动您的应用程序
ProcessBuilder pb = new ProcessBuilder("your app command");
Map<String,String> envMap = pb.environment();
envMap.put("propName","propValue");
pb.start();
4.在你的应用中使用 JNI 或原生命令加上 java 代理
//in premain
JNI[set system environment]
or
invoke native command[set system environment]
5.使用“java.lang.ProcessEnvironment”加上java代理来搞定
//in premain
Class<?> pe = Class.forName("java.lang.ProcessEnvironment");
Method getenv = pe.getDeclaredMethod("getenv",String.class);
getenv.setAccessible(true);
Field props = pe.getDeclaredField("theCaseInsensitiveEnvironment");
props.setAccessible(true);
Map<String,String> env = (Map<String,String>) props.get(null);
env.put("propName","propValue");