简体   繁体   中英

JavaFX change Button text depending on window size

Im using Scene builder for simple Mainmenu JavaFX app. THe basic window Resolution is 800*500. How can i get the text on button to change to image(or other text) after i resize window to width 200?

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="500.0" prefWidth="800.0" styleClass="bodybg" stylesheets="@../mainmenu.css" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
   <children>
      <GridPane prefHeight="500.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints maxHeight="306.0" minHeight="10.0" prefHeight="128.0" vgrow="SOMETIMES" />
            <RowConstraints maxHeight="300.0" minHeight="10.0" prefHeight="90.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="300.0" minHeight="10.0" prefHeight="90.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="300.0" minHeight="10.0" prefHeight="90.0" vgrow="SOMETIMES" />
          <RowConstraints maxHeight="300.0" minHeight="10.0" prefHeight="120.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Button fx:id="btnFirst" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="75.0" prefWidth="200.0" styleClass="firstButton" stylesheets="@../mainmenu.css" text="Hra dvou hráčů" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
               <GridPane.margin>
                  <Insets />
               </GridPane.margin>
               <font>
                  <Font size="18.0" />
               </font></Button>
            <Button fx:id="btnSecond" alignment="CENTER" contentDisplay="CENTER" mnemonicParsing="false" prefHeight="75.0" prefWidth="200.0" text="Hra proti počítači" GridPane.halignment="CENTER" GridPane.rowIndex="2" GridPane.valignment="CENTER">
               <GridPane.margin>
                  <Insets />
               </GridPane.margin>
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
            <Button fx:id="btnThird" mnemonicParsing="false" prefHeight="75.0" prefWidth="200.0" text="Hra po internetu" GridPane.halignment="CENTER" GridPane.rowIndex="3" GridPane.valignment="CENTER">
               <font>
                  <Font size="18.0" />
               </font></Button>
            <Button fx:id="btnFourth" mnemonicParsing="false" onAction="#btnEnd" prefHeight="75.0" prefWidth="200.0" styleClass="endButton" stylesheets="@../mainmenu.css" text="Konec" GridPane.halignment="CENTER" GridPane.rowIndex="4" GridPane.valignment="CENTER">
               <GridPane.margin>
                  <Insets top="-30.0" />
               </GridPane.margin>
               <font>
                  <Font size="18.0" />
               </font>
            </Button>
         </children>
      </GridPane>
   </children>
</AnchorPane>

Im using AnchorPane,GridPane and Buttons as showed above.

You could use a Window Resize Event to change the font size of your button's text with respect to the width of the screen.

fontSize = ((Width of screen)/10)) - 10;

Just an idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM