【问题标题】:How to set a font to a Timeline in javafx如何在 javafx 中将字体设置为时间轴
【发布时间】:2016-12-18 14:01:48
【问题描述】:

我正在制作一个闹钟类的程序,我需要一种方法来使钟面具有特定的字体。我以多种方式尝试了多次。如果这不可能,您能否提供另一种解决方案?提前谢谢!

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

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

【问题讨论】:

    标签: java javafx


    【解决方案1】:

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


    相关帖子:

    【讨论】:

    • 为了改变字体我必须做一个标签,而时钟部分没有标签。 setText 需要一个不能更改字体的字符串。我从 setFont 和 setStyle 技术的反复试验中知道这一点。但是,我可以使用 CSS 来更改动画(时间线)的字体吗?如果是这样,我将如何注释 java 程序以便我可以将它与 css 连接。顺便说一句,我的场景构造函数已经有了一个父类,时钟。因此,如果有一种方法可以绕过将时钟作为父类并将其设置为 CSS 的 Group() 的需要。非常感谢您的回答 GOXR3...
    • @Jake ArmStrong class DigitalClock extends Label 所以有一个 Label ,尝试在类 DigitalClock 的构造函数中添加这一行 setStyle("-fx-background-color:black; -fx-text-fill:white; -fx-font-weight:bold; -fx-border-color:white;"); 关于如何在您的应用程序上使用外部 css 的所有其他内容都在回答,我修改了底部的链接。
    猜你喜欢
    • 2012-04-26
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    相关资源
    最近更新 更多