Java FX:内联setStyle不适用于fontfamily

时间:2018-02-26 10:48:12

标签: javafx

该样式将应用于font-size,boldstyle,italic样式,但不适用于font-family。 这是代码的语法。

    public void setFontFamily() {
        String family = (String) fontFamily.getValue();
        textPreview.setText(family);
        fontName = "-fx-font-family:" + (String) fontFamily.getValue() + ";";
        previewText = "-fx-text:" + (String) fontFamily.getValue() + ";";
        setFontPreviewProperties();
        model.setFontValue(family);
    }

private void setFontPreviewProperties() {
        javafx.scene.text.Font selectedFont = javafx.scene.text.Font.font((String) fontFamily.getValue(), fontWeight,
                fontPosture, size);
        model.setFontFamily(selectedFont);
        textPreview.setFont(selectedFont);
        previewStyle = fontSize +fontName+ previewText+ boldStyle + italicStyle + underlineStyle;// fontName is not working here, everything else is working.
        textPreview.setStyle(previewStyle);
    }

1 个答案:

答案 0 :(得分:0)

这是一个示例应用程序,可能会帮助您。

import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication127 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        FontWeight fontWeight = FontWeight.BOLD;
        FontPosture fontPosture = FontPosture.ITALIC;
        double fontSize = 12;

        TextField textField = new TextField();
        TextArea textArea = new TextArea();
        VBox vBox = new VBox(textField, textArea);

        ListView<String> listView = new ListView();
        List<String> familiesList = Font.getFamilies();
        ObservableList<String> familiesObservableList = FXCollections.observableArrayList(familiesList);
        listView.setItems(familiesObservableList);
        listView.getSelectionModel().selectedItemProperty().addListener((Obs, oldFamily, newFamily) -> {
            Font font = Font.font(newFamily, fontWeight, fontPosture, fontSize);//This is probably all you need.
            textField.setText(newFamily);
            textArea.setText(newFamily + System.lineSeparator() + System.lineSeparator() + "Pack my box with five dozen liquor jugs");
            textArea.setFont(font);
        });

        HBox root = new HBox(listView, vBox);

        Scene scene = new Scene(root, 700, 650);
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}