2016-12-17 80 views
2

我正在製作鬧鐘類程序,我需要一種方法使時鐘面對特定的字體。我多次嘗試過多次。如果這是不可能的,請您提供另一種解決方案?先謝謝你!如何將字體設置爲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(); 
    } 

我也知道一些進口沒有用,我寧願保留它們。再次感謝!

回答

0

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


相對帖子:

+0

爲了改變字體我必須做出一個標籤,並有在時鐘部分沒有標籤。 setText需要一個字符串,不能在字體中更改。我從setFont&setStyle技術的嘗試和錯誤中知道這一點。但是,我可以使用CSS來更改動畫(時間線)的字體嗎?如果是這樣,我將如何註釋Java程序,以便我可以將它與CSS連接。順便說一句,我的場景構造函數已經有一個父類,時鐘。因此,如果有一種方法可以繞過將時鐘作爲父類並將其設置爲Group()以獲取css的需要。我非常感謝你的回答GOXR3 ... –

+0

@Jake ArmStrong'class DigitalClock extends Label',所以有一個'Label',嘗試在類DigitalClock的Constructor中添加這行'setStyle(「 - fx-background-color:black ; -fx-text-fill:white; -fx-font-weight:bold; -fx-border-color:white;「);其他所有關於如何在應用程序中使用外部css的信息都在答案中,我修改了底部的鏈接。 – GOXR3PLUS

相關問題