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

时间:2016-12-17 23:08:43

标签: java 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();
    }

我也知道有些导入没有使用,我宁愿保留它们。再次感谢!

1 个答案:

答案 0 :(得分:0)

Font与您在示例中使用的Label有关。

&lt; -----------------做的方法-----------------&gt;

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文档中):here

相关帖子: