Java FX: Bold and Italic styles are not getting applied for some of the font families

别来无恙 提交于 2020-06-28 04:42:06

问题


While applying bold and italic style to all font families, the bold and italic styles gets applied to some of the fonts. In some of the cases like Algerian the bold-italic style does not applied. Is there any way for handling these special cases for Algerian and some similar font families for which bold-italic style does not get applied?

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class FontTest extends Application {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        launch();

    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        // TODO Auto-generated method stub
        Text t=new Text();
        t.setText("Abc");
        t.setX(50); 
          t.setY(50);
          Font font = Font.font("Algerian", FontWeight.BOLD, FontPosture.ITALIC, 19);

          t.setFont(font);
         Group group = new Group(t); 
          //Creating a Scene by passing the group object, height and width   
           Scene scene = new Scene(group ,200, 200); 
           primaryStage.setTitle("Sample Application"); 

           //Adding scene to the stage 
           primaryStage.setScene(scene); 

           //Displaying the contents of the stage 
           primaryStage.show();
    }

}

回答1:


Not all fonts have support for multiple font weights and posture, and Algerian font belongs to the group that has only one single style. If the font itself doesn't know how to render texts in a bold or italic manner, there is no way JavaFX would magically know how to do so.

If you need to always display bold or italic texts, then you must use a font that supports that. If you are writing or designing a module, you should, as far as possible, avoid exposing the capability to change the font. If you hold the only control over the font, and you know those fonts support bold or italic styles, then you can be sure that your application will always display texts in bold or italics.



来源:https://stackoverflow.com/questions/49042348/java-fx-bold-and-italic-styles-are-not-getting-applied-for-some-of-the-font-fam

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!