簡體   English   中英

如何在javafx中將字體設置為時間軸

[英]How to set a font to a Timeline in javafx

我正在制作一種鬧鍾類型的程序,我需要一種使時鍾表面成為特定字體的方法。 我已經以多種方式嘗試了多次。 如果那不可能,請您提供其他解決方案? 先感謝您!

import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.print.DocFlavor.URL;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Menu extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {

    //Frame stuff (works)
    GridPane grid = new GridPane();
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);

    //Frame Size
    Scene scene = new Scene(new DigitalClock(),1080, 720);

    //Icon
    primaryStage.getIcons().add(new Image(getClass().getResourceAsStream(("/lib/logo.png"))));
    primaryStage.setTitle("Clock: 140 Edition");

    //Necessities
    primaryStage.setScene(scene);
    primaryStage.show();
}

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

}

class Util {
    public static String pad(int fieldWidth, char padChar, String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = s.length(); i < fieldWidth; i++) {
          sb.append(padChar);
        }
        sb.append(s);

        return sb.toString();
        }
}

class DigitalClock extends Label {

    public DigitalClock() {
        bindToTime();
        }

    // the digital clock updates once a second.
    private void bindToTime() {
    Timeline timeline = new Timeline(
      new KeyFrame(Duration.seconds(0),
        new EventHandler<ActionEvent>() {
          @Override public void handle(ActionEvent actionEvent) {
            Calendar time = Calendar.getInstance();
            String hourString = Util.pad(2, ' ', time.get(Calendar.HOUR) == 0 ? "12" : time.get(Calendar.HOUR) + "");
            String minuteString = Util.pad(2, '0', time.get(Calendar.MINUTE) + "");
            String secondString = Util.pad(2, '0', time.get(Calendar.SECOND) + "");
            String ampmString = time.get(Calendar.AM_PM) == Calendar.AM ? "AM" : "PM";
            setText(hourString + ":" + minuteString + ":" + secondString + " " + ampmString);
          }
        }
      ),
      new KeyFrame(Duration.seconds(1))
    );
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();
    }

我也知道某些進口未使用,我希望保留它們。 再次感謝!

Font Font與示例中使用的Label有關。

<-----------------方法----------------->


1)使用外部CSS

/*The font path*/
@font-face{
    src: url("../fonts/Younger than me Bold.ttf");
}

/* An element which has this id*/
#LabelID{
 -fx-font-family:"Younger than me";
 -fx-font-size:18.0;
}

//or for all labels
.label{
  -fx-font-family:"Younger than me";
  -fx-font-size:18.0;
}

2)使用setStyle(...)

`label.setStyle("-fx-font-family:monospace; -fx-font-size:16px; -fx-text-fill:black; -fx-border-color:red;");`

3)使用setFont(...)

b.setFont(new Font("Arial", 24));

4)棘手且不推薦使用(不包括在JavaFX文檔中): 此處


相對職位:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM