简体   繁体   English

如何在javafx中将字体设置为时间轴

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

I am making an alarm clock kind of program and I need a way to make the clock face a specific font. 我正在制作一种闹钟类型的程序,我需要一种使时钟表面成为特定字体的方法。 I have tried multiple times in multiple ways. 我已经以多种方式尝试了多次。 If that is not possible can you please provide another solution? 如果那不可能,请您提供其他解决方案? Thank you in advance! 先感谢您!

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();
    }

I also know some of the imports are not used, I would prefer to keep them. 我也知道某些进口未使用,我希望保留它们。 Thanks again! 再次感谢!

🍒The Font has to do with the Label you are using in the example. Font Font与示例中使用的Label有关。

<-----------------Ways to do-----------------> <-----------------方法----------------->


1)Using external css : 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)Using setStyle(...) : 2)使用setStyle(...)

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

3)Using setFont(...) : 3)使用setFont(...)

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

4)Tricky and not recommended ( not included in JavaFX docs) : here 4)棘手且不推荐使用(不包括在JavaFX文档中): 此处


Relative posts: 相对职位:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM