JavaFX自定义字体
实际上到处都在搜索,但任何答案都帮不了我,所以问题是: 我想为我的按钮添加自定义字体。我已经尝试过编写一个css java类和许多其他解决方案,但没有任何帮助 /删除代码/ 如果有人能帮助我,我将非常高兴 更新: 我试过这个:JavaFX自定义字体,java,fonts,javafx,javafx-8,Java,Fonts,Javafx,Javafx 8,实际上到处都在搜索,但任何答案都帮不了我,所以问题是: 我想为我的按钮添加自定义字体。我已经尝试过编写一个css java类和许多其他解决方案,但没有任何帮助 /删除代码/ 如果有人能帮助我,我将非常高兴 更新: 我试过这个: package view; import ... public class SceneStyle { ImageView header = new ImageView("/view/images/header.jpg"); ImageView chatImg = new
package view;
import ...
public class SceneStyle {
ImageView header = new ImageView("/view/images/header.jpg");
ImageView chatImg = new ImageView("/view/images/chat_win.jpg");
//Layout Style
//public String font1 = "Impact";
//public String font2 = "Comic Sans MS";
public static String color1 = "#00adf0";
public static String color2 = "#0076a3";
public void setLabelStyle(Label label) {
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.GRAY);
}
public void setLabelStyle2(Label label){
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.GRAY);
}
public void setLabelStyle3(Label label){
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.RED);
}
public void setButtonStyle(Button button){
button.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 30; -fx-font-style: normal");
Scene scene = new Scene(button);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
button.setTextFill(Color.web(color2));
button.setPrefWidth(190);
}
public void setBorderPaneStyle(BorderPane pane, VBox btnBox, HBox scoreBox){
pane.setTop(header);
pane.setLeft(btnBox);
pane.setRight(chatImg);
pane.setBottom(scoreBox);
pane.getLeft().setStyle("-fx-background-image: url(\"/view/images/border_left.jpg\");");
pane.getBottom().setStyle("-fx-background-image: url(\"/view/images/bottom.jpg\");");
//pane.getCenter().setStyle("-fx-background-image: url(\"/view/images/center.jpg\");");
//TODO: why can't I implement this?
}
public static String getFontColor1(){ return color1; }
public static String getFontColor2(){ return color2; }
public void setLabelStyle(Label label) {
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.GRAY);
谢谢你的详细回答,但实际上我不想写一个被覆盖的start方法。只是想在我的代码中实现字体。是的,现在我正在尝试谷歌字体。
谢谢你回答我的问题 下面是一个小示例,演示如何加载和设置自定义字体
package createfont;
import java.io.IOException;
import java.io.InputStream;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
public class CustomFontTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
String currentFontFile = "English Gothic, 17th c..TTF";
InputStream fontStream = CustomFontTest.class.getResourceAsStream(currentFontFile);
if (fontStream != null) {
Font bgFont = Font.loadFont(fontStream, 36);
fontStream.close();
final Button button = new Button("Press me");
button.setFont(bgFont);
BorderPane root = new BorderPane();
root.setCenter(button);
Scene scene = new Scene(root, 500, 100);
primaryStage.setTitle("CustomFontTest");
primaryStage.setScene(scene);
primaryStage.show();
} else {
throw new IOException("Could not create font: " + currentFontFile);
}
}
public static void main(String[] args) {
launch(args);
}
}
下面是如何在JavaFX应用程序中使用自定义字体的简单示例。此示例只是示例的编辑版本 输出
@font-face {
font-family: 'Fleftex';
src: url('fonts/Fleftex_M.ttf');
}
.label {
-fx-font-family: 'Fleftex';
-fx-font-size: 20;
}
.button .text {
-fx-font-family: 'Fleftex';
}
项目结构 下图是项目结构
Java类
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FontLoad extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Label label = new Label("JavaFX Application");
Button button = new Button("My Button");
VBox box = new VBox(15, label, button);
box.setAlignment(Pos.CENTER);
Scene scene = new Scene(box, 500, 300);
scene.getStylesheets().add(getClass().getResource("/fontstyle.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
样式表
@font-face {
font-family: 'Fleftex';
src: url('fonts/Fleftex_M.ttf');
}
.label {
-fx-font-family: 'Fleftex';
-fx-font-size: 20;
}
.button .text {
-fx-font-family: 'Fleftex';
}
更新>=8u60 从这个版本开始,“字体系列”属性被忽略,您必须使用TTF的真实名称 举例:要使用文件Fleftex_M.ttf中包含的字体“Fleftex”,必须使用此CSS文件:
@font-face {
src: url(“fonts/Fleftex_M.ttf”);
}
.label {
-fx-font-family: 'Fleftex';
}
我试过这个:
package view;
import ...
public class SceneStyle {
ImageView header = new ImageView("/view/images/header.jpg");
ImageView chatImg = new ImageView("/view/images/chat_win.jpg");
//Layout Style
//public String font1 = "Impact";
//public String font2 = "Comic Sans MS";
public static String color1 = "#00adf0";
public static String color2 = "#0076a3";
public void setLabelStyle(Label label) {
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.GRAY);
}
public void setLabelStyle2(Label label){
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.GRAY);
}
public void setLabelStyle3(Label label){
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.RED);
}
public void setButtonStyle(Button button){
button.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 30; -fx-font-style: normal");
Scene scene = new Scene(button);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
button.setTextFill(Color.web(color2));
button.setPrefWidth(190);
}
public void setBorderPaneStyle(BorderPane pane, VBox btnBox, HBox scoreBox){
pane.setTop(header);
pane.setLeft(btnBox);
pane.setRight(chatImg);
pane.setBottom(scoreBox);
pane.getLeft().setStyle("-fx-background-image: url(\"/view/images/border_left.jpg\");");
pane.getBottom().setStyle("-fx-background-image: url(\"/view/images/bottom.jpg\");");
//pane.getCenter().setStyle("-fx-background-image: url(\"/view/images/center.jpg\");");
//TODO: why can't I implement this?
}
public static String getFontColor1(){ return color1; }
public static String getFontColor2(){ return color2; }
public void setLabelStyle(Label label) {
label.setStyle("-fx-font-family: Inconsolata:700; -fx-font-size: 25");
Scene scene = new Scene(label);
scene.getStylesheets().add("https://fonts.googleapis.com/css?family=Inconsolata:700");
label.setTextFill(Color.GRAY);
在每种方法中。因为我不想写一个像上面那样重写start方法的新方法。(事实上,感谢您提供了这个详细的答案,但这不是我想要的)。
但是我的解决方案也不太好…我每次尝试@font-face时,它都不适合我。您必须使用font.loadFont(…)在代码上加载字体。然后您可以在css上使用它。您可以尝试使用它,它使用
@font-face
加载一个新的字体文件,然后将其应用于按钮和标签控件。有一个定义是使用css@font-face
。您提到您不想编写被覆盖的开始方法。但是,在纯文本中您需要这样一个方法,否则您将无法让JavaFX执行任何操作,即使没有start方法也不行。您只需要为应用程序添加一次给定字体的样式表。您不需要在每次向UI添加元素(例如每个按钮和每个标签)时都添加相同的css样式表。你也不需要为每个元素创建一个新的场景,你通常只有一个场景,你可以将所有可见的元素添加到其中。实际上你没有得到“英国哥特式,17世纪,TFF”?因为它不是文件路径或正确的文件名。当然不是。我的假设是,您可以将其替换为您自己的任何字体文件。您不必每次都重写start()
。在扩展应用程序的类中执行一次。样式表通常只添加一次到场景中。可能人们在这里看不到的是,@font-face
在css文件中排在第一位并非偶然。这实际上是它需要工作的地方,否则它将被忽略。经过一个小时的艰苦搜索,我终于明白了为什么我的字体被忽略了