How to fix changing fontawesomefx icons to a rectangle when the font size is specified in JavaFX?

断了今生、忘了曾经 提交于 2019-12-11 01:34:05

问题


I'm using fontawesomefx-8.9 and I have set their font size to 15px using

.glyph-icon {
  -fx-font-size: 15px;
}

Then I have created some FontAwesomeIconViews and embed them in some Buttons(JavaFX).

The issue I had to face is changing fontAwesome icons to a rectangle when hovering mouse over the button.

FXML File is below(FXMLDocument,fxml)

<?xml version="1.0" encoding="UTF-8"?>

<?import de.jensd.fx.glyphs.fontawesome.FontAwesomeIconView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" stylesheets="@styles.css" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111" fx:controller="test_fontawesome.FXMLDocumentController">
    <children>
        <Button fx:id="button" contentDisplay="TOP" layoutX="44.0" layoutY="41.0" onAction="#handleButtonAction" text="Click Me!" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="40.0">
         <graphic>
            <FontAwesomeIconView fill="BLUE" glyphName="CUT" />
         </graphic></Button>
        <Label fx:id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" />
      <Button fx:id="button1" contentDisplay="TOP" layoutX="183.0" layoutY="41.0" onAction="#handleButtonAction" text="Click Me!" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="40.0">
         <graphic>
            <FontAwesomeIconView fill="BLUE" glyphName="PLUS" />
         </graphic>
      </Button>
    </children>
</AnchorPane>

CSS is file below(styles.css)

.root {
    -fx-font-size: 12px ;
}

Just preview it on scene builder, the issue will appear!

If you want the source code, get it from here.


回答1:


i had the same issue sometime back .. i remember adding the following line to my css file fixed the issue.

.glyph-icon{ -fx-font-family:"Material Design Icons"; }

Looks like your are using FontAwesome icon , may be try with the appropriate font family for that.

  .glyph-icon{ -fx-font-family:"FontAwesome"; }



回答2:


You should consider to switch to the latest version of FontAwesomeFX. Yesterday I released 8.14: http://www.jensd.de/wordpress/?p=2579 8.14 comes with a fix of these resizing issues!




回答3:


If you change the font-size, you must set the font-family for icon.

.glyph-icon {
  -fx-font-size: 15px;
  -fx-font-family: "FontAwesome"; /* or Material Design Icons*/
}

This fix my issue! ;)



来源:https://stackoverflow.com/questions/41945099/how-to-fix-changing-fontawesomefx-icons-to-a-rectangle-when-the-font-size-is-spe

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