【问题标题】:How To Enable On Click Button Formatting, and Increase Text Size With Window如何启用单击按钮格式,并使用窗口增加文本大小
【发布时间】:2019-07-25 01:51:44
【问题描述】:

我正在尝试解决一个计算器项目的问题,我正在使用 Scene Builder 处理 javafx 11 和 java11。我不知道如何使按钮中的文本字体随着窗口/按钮大小的增加而增加。我能够使按钮响应但不是按钮内的文本。我有使用 fxml 构建的场景,并且我的大部分代码都在一个控制器类中。

为了使按钮具有响应性,我将它们放在锚窗格中,该锚窗格附加到锚定到网格窗格的锚窗格中。我一直找不到更改文本大小的方法或控件。

Main.java:

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Parent root = 
            FXMLLoader.load(getClass().getResource("calculator.fxml"));
        primaryStage.getIcons().add(new Image("CALC.png"));
        primaryStage.setTitle("TS Calculator");
        primaryStage.setScene(new Scene(root, 250, 375));
        primaryStage.setResizable(true);
        primaryStage.setMinHeight(375);
        primaryStage.setMinWidth(250);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

FXML:(仅显示其中一个按钮)

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>

<GridPane maxHeight="1.7976931348623157E308" 
maxWidth="1.7976931348623157E308" minHeight="306.00" minWidth="204.0"
          prefHeight="288.0" prefWidth="208.0" style="-fx-background- 
color: DARKSLATEGREY; -fx-border-color: green;"
          stylesheets="@styles.css" 
xmlns="http://javafx.com/javafx/11.0.1" 
xmlns:fx="http://javafx.com/fxml/1" 
fx:controller="com.trevorsmith.Controller"
          onKeyReleased="#acceptKeyboardInput" >
   <AnchorPane prefHeight="200.0" prefWidth="200.0" 
GridPane.columnSpan="4" GridPane.hgrow="ALWAYS" GridPane.vgrow="ALWAYS" 
onKeyPressed="#acceptKeyboardInput">
      <children>
                <TextField fx:id="textFieldDisplay"  editable="false" 
alignment="CENTER_RIGHT" maxHeight="1.7976931348623157E308" 
maxWidth="1.7976931348623157E308" prefWidth="196.0" 
AnchorPane.bottomAnchor="2.0" AnchorPane.leftAnchor="2.0" 
AnchorPane.rightAnchor="2.0" AnchorPane.topAnchor="2.0" />
      </children>
   </AnchorPane>
   <AnchorPane maxHeight="1.7976931348623157E308" 
maxWidth="1.7976931348623157E308" prefHeight="200.0" prefWidth="200.0" 
GridPane.hgrow="ALWAYS" GridPane.rowIndex="1" GridPane.vgrow="ALWAYS">
      <children>
          <Button maxHeight="1.7976931348623157E308" 
maxWidth="1.7976931348623157E308" text="0" AnchorPane.bottomAnchor="0.0" 
AnchorPane.leftAnchor="2.0" AnchorPane.rightAnchor="0.0" 
AnchorPane.topAnchor="0.0" />
      </children>

CSS:

Button{
-fx-background-color: black;
-fx-text-align: center;
-fx-text-fill: white;
-fx-border-color: green;
-fx-font-size: 1em;
-fx-border-radius: 10 10 10 10;
-fx-background-radius: 10 10 10 10;
}
Button:pressed{
-fx-background-color: green;
}
TextField{
-fx-font-size: 1.5em;
}

我以为字体会随着按钮大小而改变,或者有一种简单的方法可以改变大小,但一直找不到。我期待有一个 FXML 或 CSS 简单的解决方案来解决这个问题。

【问题讨论】:

    标签: java javafx fxml java-11


    【解决方案1】:

    试试. - class selector

    .button {
    ...
    -fx-font-size:40;
    }
    

    因此它将适合所有buttons 中的size

    您可以在官方文档CSS Style中阅读更多内容

    使用绑定更新示例

    import javafx.application.Application;
    import javafx.beans.binding.Bindings;
    import javafx.beans.property.DoubleProperty;
    import javafx.beans.property.IntegerProperty;
    import javafx.beans.property.SimpleDoubleProperty;
    import javafx.beans.property.SimpleIntegerProperty;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextArea;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class FontBind extends Application {
    
        private DoubleProperty fontSize = new SimpleDoubleProperty(10);
        private IntegerProperty blues = new SimpleIntegerProperty(50);
    
        @Override
        public void start(Stage primaryStage) {
            Button btn = new Button("click me, I change color");
            btn.setOnAction((evt)->{blues.set(blues.get()+20);});//max?
            Label lbl = new Label("I'm a label");
            TextArea ta = new TextArea("Lots of text can be typed\nand even number 1234567890");
            HBox hbox = new HBox(new Label("I never change"));
            VBox child = new VBox(btn, lbl, ta);
            VBox root = new VBox(child, hbox);
            Scene scene = new Scene(root, 300, 250);
    
            fontSize.bind(scene.widthProperty().add(scene.heightProperty()).divide(50));
            child.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize.asString(), ";"
                                                      ,"-fx-base: rgb(100,100,",blues.asString(),");"));
    
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    此代码来自 brian,请参阅他的帖子 Bind Font Size in JavaFX?

    【讨论】:

    • 我可以通过这种方式更改 css 中的字体大小,但我找不到使字体随着场景的宽度/高度而增加/减少的方法。我根据建议进行了更改,但按钮中的字体大小没有改变。
    • 您可以通过ListenerBindingsJavaFx 中执行此操作
    猜你喜欢
    • 2019-11-07
    • 2015-02-11
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 2013-08-31
    • 1970-01-01
    相关资源
    最近更新 更多