Ubuntu-Javafx自定义
This_is_Y Lv6

javafx局部设置字体

以MysqlMonitor(https://github.com/fupinglee/MySQLMonitor)为例

源代码结构如下:

image-20230913155450349

控件排布是通过加载main.fxml文件来实现的,修改整个的字体需要在main.fxml文件在中修改,这里我使用的加载css的方法修改,

setfont.css

1
2
3
4
.custom-font {
-fx-font-family: 'Sarasa Mono SC';
-fx-font-size: 16px;
}

修改后的main.fxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="600.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.MainController" stylesheets="setfont.css">
<children >
<Label styleClass="custom-font" layoutX="37.0" layoutY="27.0" text="Host:" />
<TextField styleClass="custom-font" fx:id="textField_host" layoutX="93.0" layoutY="22.0" prefWidth="178.0" text="127.0.0.1" />
<Label styleClass="custom-font" layoutX="284.0" layoutY="27.0" text="Port:" />
<TextField styleClass="custom-font" fx:id="textField_port" layoutX="323.0" layoutY="22.0" prefHeight="27.0" prefWidth="113.0" text="3306" />
<Label styleClass="custom-font" layoutX="452.0" layoutY="27.0" text="User:" />
<TextField styleClass="custom-font" fx:id="textField_user" layoutX="495.0" layoutY="23.0" prefHeight="27.0" prefWidth="136.0" text="root" />
<Label styleClass="custom-font" layoutX="642.0" layoutY="27.0" text="Pass:" />
<CheckBox fx:id="cb_showPass" layoutX="846.0" layoutY="27.0" mnemonicParsing="false" text="显示" />
<TextField styleClass="custom-font" fx:id="textField_password" layoutX="680.0" layoutY="22.0" prefHeight="27.0" prefWidth="157.0" text="root" />
<Button styleClass="custom-font" fx:id="testConn" layoutX="39.0" layoutY="66.0" mnemonicParsing="false" text="数据库连接" />
<Button styleClass="custom-font" fx:id="btn_logOn" layoutX="152.0" layoutY="66.0" mnemonicParsing="false" text="下断" />
<Button styleClass="custom-font" fx:id="btn_update" layoutX="244.0" layoutY="66.0" mnemonicParsing="false" text="更新" />

<Button styleClass="custom-font" fx:id="btn_clear" layoutX="329.0" layoutY="66.0" mnemonicParsing="false" text="清空" />
<Label styleClass="custom-font" layoutX="452.0" layoutY="71.0" prefHeight="17.0" prefWidth="50.0" text="搜索:" />
<TextField styleClass="custom-font" fx:id="textField_filter" layoutX="495.0" layoutY="66.0" />
<TableView styleClass="custom-font" fx:id="tableView" layoutX="25.0" layoutY="109.0" prefHeight="433.0" prefWidth="955.0">
<columns>
<TableColumn styleClass="custom-font" fx:id="tableCol_id" prefWidth="65.0" resizable="false" text="ID" />
<TableColumn styleClass="custom-font" fx:id="tableCol_date" prefWidth="160.0" resizable="false" text="Date" />
<TableColumn styleClass="custom-font" fx:id="tableCol_sql" prefWidth="730.0" text="SQL" />
</columns>
</TableView>
<Label styleClass="custom-font" fx:id="label_date" layoutX="37.0" layoutY="560.0" prefHeight="17.0" prefWidth="300.0" />
<Label styleClass="custom-font" fx:id="label_state" layoutX="650.0" layoutY="560.0" prefHeight="17.0" prefWidth="300.0" />
</children>
</AnchorPane>

主要是批量添加了 styleClass=”custom-font” 和 stylesheets=”setfont.css”

修改后的根据界面:

image-20230913160611893

对比一下之前的:

image-20230913160745460

javafx全局设置字体

直接上代码吧

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Util.Utils;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.util.Objects;

public class main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {

Parent root = FXMLLoader.load(getClass().getResource("/main.fxml"));
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/logo.png")));
primaryStage.setTitle("Multiple Database Utilization Tools - " + Utils.getCurrentVersion());
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("setfont.css").toExternalForm());
primaryStage.setScene(scene);

primaryStage.show();
}

public static void main(String[] args) {
launch(args);
}
}

image-20230914151341321

原代码来自:MDUT:https://github.com/SafeGroceryStore/MDUT

setfont.css

1
2
3
4
5
.root {
-fx-font-family: 'Sarasa Mono SC';
-fx-font-size: 16px;
-fx-text-fill: red;
}

原来的代码是:

1
primaryStage.setScene(new Scene(root));

在scene被primaryStage.setScene之前,给他加上一个getStylesheets().add()

Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("setfont.css").toExternalForm());
primaryStage.setScene(scene);

其他的用到了scene的地方也要一样的操作,不过不同的fxml文件创建的scene,要用不同的css文件(内容可以一样)

如下图:

image-20230914155835918

对比,修改后:

image-20230914151750271

修改前:

image-20230914151651497

 Comments