如何补偿某人在计算机上没有所需的字体?

时间:2016-12-18 21:32:42

标签: java

我在java中制作了一个使用特定字体(Digital Dream Fat)的桌面时钟应用程序,以达到理想的外观,但如果有人没有在他们的计算机上安装该字体,它将默认为Arial 。我该如何弥补?

这是我的代码:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Tooltip;
import javafx.stage.StageStyle;
import javafx.scene.paint.Color;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.util.Duration;

    public class Clock extends Application {

        public Text text;

            @Override 
            public void start(Stage stage) {

                    DateFormat df = new SimpleDateFormat("EEE,MMM d yyyy - h:mm:ss a");
                    Date date = new Date();
                    String stringDate = df.format(date);

                    text = new Text(10, 60, stringDate);
                    text.setFont(Font.font ("Digital Dream Fat", 30f));
                    text.setFill(Color.RED);


            Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0), new EventHandler<ActionEvent>() {

                    @Override
                    public void handle(ActionEvent actionEvent) {
                    Date update = new Date();
                    String stringNewDate = df.format(update);
                    text.setText(stringNewDate);
                    }
            }), new KeyFrame(Duration.seconds(1)));

            timeline.setCycleCount(Animation.INDEFINITE);
            timeline.play();

                    ChoiceBox colorChoice = new ChoiceBox(FXCollections.observableArrayList("Red", "Blue", "Green", "Grey", "Black", "White"));

                    colorChoice.setOnAction(new EventHandler<ActionEvent>(){
                        public void handle(ActionEvent e) {
                            if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Red")) {
                            colorChoice.setStyle("-fx-base: red");
                            text.setFill(Color.RED);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Blue")) {
                            colorChoice.setStyle("-fx-base: blue");
                            text.setFill(Color.BLUE);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Green")) {
                            colorChoice.setStyle("-fx-base: green");
                            text.setFill(Color.GREEN);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Grey")) {
                            colorChoice.setStyle("-fx-base: grey");
                            text.setFill(Color.GREY);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("White")) {
                            colorChoice.setStyle("-fx-base: white");
                            text.setFill(Color.WHITE);
                            } else if (colorChoice.getSelectionModel().getSelectedItem().toString().equals("Black")) {
                            colorChoice.setStyle("-fx-base: black");
                            text.setFill(Color.BLACK);
                            } else {
                            colorChoice.setStyle("-fx-base: red");
                            text.setFill(Color.RED);
                            }
                        }
                    });

                    HBox hbox = new HBox(colorChoice);

                    colorChoice.setTooltip(new Tooltip("Select A Colour"));
                    colorChoice.getSelectionModel().selectFirst();


                    Scene scene = new Scene(new Group(text, hbox));
                    scene.setFill(Color.TRANSPARENT);

                    stage.setScene(scene);
                    stage.initStyle(StageStyle.TRANSPARENT);
                    stage.setX(0);
                    stage.setY(0);
                    stage.setWidth(710);
                    stage.setHeight(80);
                    stage.show(); 
            }

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

0 个答案:

没有答案