[英]System.setProperty("prism.allowhidpi", "false") does not work?
我正在嘗試在我的 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();
}
}
編輯:如評論中所述,創建一個單獨的啟動器 class 設置屬性,然后調用應用程序 class 工作。 但是,它似乎不適用於 Gluon Substrate。 這是我項目的相關部分。 我還添加了另外兩個棱鏡屬性,以確保新的主 class 在 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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.