Home>

I am studying JavaFX for java8 with eclipse.
of the following code In the code of "Height label and slider"
Please let me know because sliderH.setPrefWidth (280);is not well understood
I think that the width of the slider is set, the width for the frame, the width in Label lblHv,
I checked the net, such as the default width for the width of Label lblHv,
I didn't understand what I was saying. I think my explanation is strange, but
Thank you.

public class BMI extends Application {
    Label lblWv = new Label ();
    Label lblHv = new Label ();
    Slider sliderH = new Slider (100, 200, 165.0);// Create a horizontal slider by specifying minimum, maximum, and initial values.
    Slider sliderW = new Slider (20, 120, 62.0);
    Label lblBMI = new Label ("BMI");
    @Override
    public void start (Stage stage) throws Exception {
        stage.setTitle ("BMI");
        stage.setWidth (400);
        stage.setHeight (230);
        MenuBar menuBar = new MenuBar ();
        menuBar.setUseSystemMenuBar (true);
        Menu fileMenu = new Menu ("File");
        MenuItem mnuExit = new MenuItem ("Exit");
        mnuExit.setOnAction (new EventHandler<ActionEvent>() {
            @Override
            public void handle (ActionEvent e) {
                System.exit (0);
            }
        });
        fileMenu.getItems (). addAll (mnuExit);
        menuBar.getMenus (). add (fileMenu);
        // Height label and slider
        Label lblH = new Label ("Height:");
        sliderH.setValue (170);
        sliderH.setPrefWidth (280);
        sliderH.setOrientation (Orientation.HORIZONTAL);
        sliderH.setShowTickMarks (true);
        sliderH.setShowTickLabels (true);
        sliderH.setMajorTickUnit (50.0f);
        sliderH.setBlockIncrement (5.0f);
        lblHv.setText (sliderH.getValue () + "cm");
        sliderH.setOnMouseClicked (event->updateValue ());
        sliderH.setOnKeyPressed (event->updateValue ());
        HBox boxH = new HBox ();
        boxH.getChildren (). addAll (lblH, sliderH, lblHv);
                ...
  • Answer # 1

    If you look at the base class javafx.scene.control.Control of UI controls including the

    button, the following properties regarding the size are defined.
    Property name Type
    height double
    width double
    minHeight double
    minWidth double
    maxHeight double
    maxWidth double
    prefHeight double
    // Property to override the calculated preferred width of the prefWidth area.
    prefWidth double

    The recommended size of the Button class (prefHeight and prefWidth) is basically determined by the length of the string set in the Button * 1, and the maximum size (maxHeight and maxWidth) is set to the same as the recommended size * 2. .