Javafx 8、scenebuilder 2和controlsfx与Fontsome?
我对javafx比较陌生,最近开始了一个使用Java8和javafx的项目。我正在使用SceneBuilder2.0构建我的JavaFXUI。我想知道是否有人在scenebuilder中使用过fontawesome?目前,我需要这样做,以添加图形的标签Javafx 8、scenebuilder 2和controlsfx与Fontsome?,javafx,java-8,scenebuilder,Javafx,Java 8,Scenebuilder,我对javafx比较陌生,最近开始了一个使用Java8和javafx的项目。我正在使用SceneBuilder2.0构建我的JavaFXUI。我想知道是否有人在scenebuilder中使用过fontawesome?目前,我需要这样做,以添加图形的标签 levelLabel1.setGraphic(create(FontAwesome.Glyph.CHEVRON_RIGHT)); public static Node create(Glyph glyph) { FontAwesome
levelLabel1.setGraphic(create(FontAwesome.Glyph.CHEVRON_RIGHT));
public static Node create(Glyph glyph) {
FontAwesome fontAwesome = new FontAwesome();
fontAwesome.fontColor(color);
Node result = fontAwesome.create(glyph.getChar());
result.setScaleX(SCALE);
result.setScaleY(SCALE);
return result;
}
您必须为它创建一个新的应用程序。 然后您将能够在那里加载它(但仍然无法在那里看到图示符),并且能够更轻松地设置图示符: Main.java
import org.controlsfx.glyphfont.FontAwesome;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
FALabel label = new FALabel();
label.setGlyph(FontAwesome.Glyph.ANDROID);
label.setValue("RoBOZUKU");
Pane p = new Pane();
p.getChildren().add(label);
Scene scene = new Scene(p);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
FALabel.java
import org.controlsfx.glyphfont.FontAwesome;
import org.controlsfx.glyphfont.FontAwesome.Glyph;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
public class FALabel extends Label{
private Color color;
private Glyph glyph;
public void setValue(String text){
this.setText(text);
this.setGraphic(create(getGlyph()));
}
public FALabel() {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FALabel.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (Exception exception) {
throw new RuntimeException(exception);
}
}
public Node create(Glyph glyph) {
FontAwesome fontAwesome = new FontAwesome();
fontAwesome.fontColor(getColor());
Node result = fontAwesome.create(glyph.getChar());
result.setScaleX(this.getScaleX());
result.setScaleY(this.getScaleY());
return result;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Glyph getGlyph() {
return glyph;
}
public void setGlyph(Glyph fontAwesome) {
this.glyph = fontAwesome;
}
}
FALabel.fxml
PS:我知道这可能不是您想要的,但场景生成器仍然非常有限
PS2:您还可以使用创建自定义组件,然后在带有图像视图和标签的窗格(AnchorPane、Pane、HBox)中实现。您可以使用FontAwesomeFX 8.1,它可以实现这一点 使用ControlsFx,您需要编辑fxml文件。()
//...
//...
我认为scenebuilder有一定的局限性,但这确实是一种更容易加载图标的方法!谢谢这是一个更好、更及时的答案。接受的答案不再是必需的。很好,为了完整,如果您希望字体脱机可用(没有CDN,并且需要在ressource中添加ttf文件),可以添加her代码:GlyphFontRegistry.register(新FontAwesome(getClass().getResourceAsStream(“/fonts/FontAwesome webfont.ttf”);说吧。@pdem answer\u200c\u200b
中有一些unicode垃圾。不要复制filename.EFernandes我将此代码与ControlsFX 8.40一起使用,但它不起作用:(.完全相同..当我将FXML加载到SceneBuilder时,它不会显示任何内容。@GOXR3PLUS您需要生成并运行您的程序。
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<fx:root type="javafx.scene.control.Label" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"/>
<?import org.controlsfx.glyphfont.*?>
//...
<Label>
<graphic>
<Glyph fontFamily="FontAwesome" icon="PLUS" />
</graphic>
</Label>
//...