我在Linux上使用IntelliJ构建了Maven项目,一切正常,但是当我在Windows 10中执行同一项目时,JFXButton
中的文本无法正确呈现(大多数按钮中都显示了一半的文本) )。
在两个系统中都使用了Java 11.0.2,IntelliJ IDEA Ultimate Edition和用于Java 11的SceneBuilder 11。
对我来说奇怪的是,它可以在SceneBuilder中正确渲染,但是当我执行它时,它弄乱了GUI。它还不能识别任何已安装的字体(在CSS中也尝试了 @ font-face 来获取 .tff 字体,但没有成功)。
在Linux(运行和Scene Builder)和Windows 10(Scene Builder)上,我得到:
在Windows 10(运行)上,我得到:
(或者使用更大的字体时更糟)
我正在为此场景使用的Fxml :(由SceneBuilder生成)
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXTextField?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="root" maxHeight="380.0" maxWidth="600.0" minHeight="380.0" minWidth="600.0" prefHeight="380.0" prefWidth="600.0" stylesheets="@../css/CloseInterruptStyle.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="mbyllNderprejTitle" layoutX="172.0" layoutY="14.0" text="Ndërprej Kontratën" />
<Label alignment="CENTER_RIGHT" layoutX="125.0" layoutY="220.0" maxHeight="26.0" maxWidth="180.0" minHeight="26.0" minWidth="170.0" prefHeight="26.0" prefWidth="180.0" styleClass="fieldSize" text="Karburanti në dorëzim :" />
<Label alignment="CENTER_RIGHT" layoutX="125.0" layoutY="255.0" maxHeight="26.0" maxWidth="180.0" minHeight="26.0" minWidth="170.0" prefHeight="26.0" prefWidth="180.0" styleClass="fieldSize" text="Kilometrat në dorëzim :" />
<JFXTextField fx:id="karburantiTextField" alignment="CENTER" focusColor="#f8ea53" layoutX="307.0" layoutY="220.0" maxHeight="26.0" maxWidth="145.0" minHeight="26.0" minWidth="145.0" prefHeight="26.0" prefWidth="145.0" styleClass="fieldSize" />
<JFXTextField fx:id="kilometraTextField" alignment="CENTER" focusColor="#f8ea53" layoutX="307.0" layoutY="255.0" maxHeight="26.0" maxWidth="145.0" minHeight="26.0" minWidth="145.0" prefHeight="26.0" prefWidth="145.0" styleClass="fieldSize" />
<JFXButton fx:id="anulloButton" buttonType="RAISED" layoutX="185.0" layoutY="316.0" minHeight="31.0" onAction="#anulloButtonAction" prefHeight="31.0" prefWidth="111.0" text="Anullo" />
<JFXButton fx:id="mbyllNderprejButton" buttonType="RAISED" layoutX="326.0" layoutY="316.0" minHeight="31.0" onAction="#mbyllNderprejButtonAction" prefHeight="31.0" prefWidth="111.0" text="Ndërprej" />
<Label alignment="CENTER_RIGHT" layoutX="125.0" layoutY="80.0" maxHeight="26.0" maxWidth="180.0" minHeight="26.0" minWidth="170.0" prefHeight="26.0" prefWidth="180.0" styleClass="fieldSize" text="Kontrata me ID :" />
<JFXTextField fx:id="kontractIdTextField" alignment="CENTER" editable="false" focusColor="#b93845" layoutX="307.0" layoutY="80.0" maxHeight="26.0" maxWidth="145.0" minHeight="26.0" minWidth="145.0" prefHeight="26.0" prefWidth="145.0" styleClass="fieldSize" unFocusColor="#b93845" />
<Label alignment="CENTER_RIGHT" layoutX="125.0" layoutY="115.0" maxHeight="26.0" maxWidth="180.0" minHeight="26.0" minWidth="170.0" prefHeight="26.0" prefWidth="180.0" styleClass="fieldSize" text="Nga Klienti :" />
<JFXTextField fx:id="klientiTextField" alignment="CENTER" editable="false" focusColor="#b93845" layoutX="307.0" layoutY="115.0" maxHeight="26.0" maxWidth="145.0" minHeight="26.0" minWidth="145.0" prefHeight="26.0" prefWidth="145.0" styleClass="fieldSize" unFocusColor="#b93845" />
<Label alignment="CENTER_RIGHT" layoutX="125.0" layoutY="150.0" maxHeight="26.0" maxWidth="180.0" minHeight="26.0" minWidth="170.0" prefHeight="26.0" prefWidth="180.0" styleClass="fieldSize" text="Koha e dorezimit : " />
<JFXTextField fx:id="kohaDorezimitTextField" alignment="CENTER" editable="false" focusColor="#b93845" layoutX="307.0" layoutY="150.0" maxHeight="26.0" maxWidth="145.0" minHeight="26.0" minWidth="145.0" prefHeight="26.0" prefWidth="145.0" styleClass="fieldSize" unFocusColor="#b93845" />
<Label alignment="CENTER_RIGHT" layoutX="125.0" layoutY="185.0" maxHeight="26.0" maxWidth="180.0" minHeight="26.0" minWidth="170.0" prefHeight="26.0" prefWidth="180.0" styleClass="fieldSize" text="Diferenca e kohës :" />
<JFXTextField fx:id="diferencaKohesTextField" alignment="CENTER" editable="false" focusColor="#b93845" layoutX="307.0" layoutY="185.0" maxHeight="26.0" maxWidth="145.0" minHeight="26.0" minWidth="145.0" prefHeight="26.0" prefWidth="145.0" styleClass="fieldSize" unFocusColor="#b93845" />
</children>
</AnchorPane>
编辑:使用此方法添加了控制器(几乎适用于该应用程序的每个 fxml 文件):
//get parent from given fxml path, and sets it controller
Parent getParentView(String fxmlPath, Object controller) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource(fxmlPath));
loader.setController(controller);
return loader.load(); //this is added to a stage or inside another container
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
我在两种情况下都使用的CSS样式:
#root {
-fx-background-color: linear-gradient(to right, #cb356b, #bd3f32);
}
#mbyllNderprejTitle {
-fx-font-size: 32;
-fx-text-fill: #F2CA00;
-fx-font-family: "Kotta One";
}
.fieldSize {
-fx-font-size: 15;
}
#anulloButton, #mbyllNderprejButton {
-fx-border-radius: 10pt;
-fx-background-radius: 10pt;
-fx-font-size: 16;
}
.text-field {
-fx-background-color: linear-gradient(to bottom, rgba(236, 240, 241, 0.03), rgba(236, 240, 241, 0.08));
-fx-font-size: 14px;
}
#anulloButton {
-fx-background-color: linear-gradient(to right, rgba(234, 215, 186, 0.36), rgba(234, 171, 135, 0.34));
}
#mbyllNderprejButton {
-fx-background-color: linear-gradient(to right, rgba(230, 134, 1, 0.58), rgba(191, 19, 77, 0.53));
-fx-text-fill: white;
}
Maven依赖项:
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
<classifier>mac</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-media</artifactId>
<version>11</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<classifier>win</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<classifier>mac</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-graphics</artifactId>
<version>11</version>
<classifier>linux</classifier>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>9.0.0</version>
</dependency>
<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>9.0.8</version>
</dependency>
<dependency>
<groupId>io.github.typhon0</groupId>
<artifactId>AnimateFX</artifactId>
<version>1.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
</dependencies>
我尝试使按钮的高度更大(以包括文本),但它至少需要 33px高度才能几乎包含 15-16px字体大小,其中“破坏了该应用程序的外观。
对于字体识别,我还没有找到解决方案。
由于客户端正在使用Windows 10,因此需要在Windows中运行此功能。是否需要其他依赖项?或者我做错了什么?
答案 0 :(得分:0)
使用以下CSS选择器之一解决了JFXButtons中的文本呈现:
.button .text {
-fx-font-size: 18;
}
.jfx-button .text {
-fx-font-size: 18;
}
#buttonId .text {
-fx-font-size: 18;
}
在每种外部字体的 start()方法中使用以下代码行来解析字体识别:
@Override
public void start(Stage stage) throws Exception {
//load all application external fonts
javafx.scene.text.Font.loadFont(
getClass().getResource("/fonts/KottaOne-Regular.ttf").toExternalForm(), 15f);
//...
}
使用以下字体可以在所有CSS文件中使用字体:
.label {
-fx-font-family: "Kotta One";
-fx-font-size: 20;
}