首页 文章

使用HBox javafx定位按钮

提问于
浏览
-2

我试图将我的两个按钮直接放在我的标签下,并将所有内容都放在我的表格上 . 目前一切都只是印在一条线上 .

@Override public void start(Stage primaryStage)抛出Exception {

HBox hb = new HBox();
    hb.setSpacing(15);
    hb.setPadding(new Insets(15, 20, 5, 10));
    hb.setAlignment(Pos.CENTER);

    Label label = new Label("Greetings! Would you like to purchase cruise ticket for a family of two?");
    hb.getChildren().add(label);

    button1 = new Button("Yes");
    hb.getChildren().add(button1);
    button1.setOnAction(this);

    button2 = new Button("No");
    hb.getChildren().add(button2);
    button2.setOnAction(this);


    Scene scene = new Scene(hb, 550, 250);
    primaryStage.setTitle("Cruise for two!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

2 回答

  • -1

    有关JavaFX中可用的许多布局选项的更多信息,Oracle提供了一个很好的教程,我建议您查看!

    HBox 将始终将其子项排列在水平行中 . 通过将 Label 添加到 HBox ,JavaFX正在做你正在告诉它的事情 .

    您还有 VBox ,它垂直排列节点 . 在 VBox 中,添加 Label ,然后添加 HBox ;他们将在 Label 下安排 HBox .

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    
    public class Main extends Application {
    
        private Button button1;
        private Button button2;
    
        public static void main(String[] args) {
            launch(args);
        }
    
        @Override
        public void start(Stage primaryStage) {
    
            VBox root = new VBox(5);
            root.setPadding(new Insets(10));
            root.setAlignment(Pos.CENTER);
    
            HBox hb = new HBox();
            hb.setSpacing(15);
            hb.setPadding(new Insets(15, 20, 5, 10));
            hb.setAlignment(Pos.CENTER);
    
            Label label = new Label("Greetings! Would you like to purchase cruise ticket for a family of two?");
            root.getChildren().add(label);
    
            button1 = new Button("Yes");
            hb.getChildren().add(button1);
    
            button2 = new Button("No");
            hb.getChildren().add(button2);
    
            root.getChildren().add(hb);
    
            Scene scene = new Scene(root, 550, 250);
            primaryStage.setTitle("Cruise for two!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    }
    

    结果:

    screenshot

  • 3
    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.*?>
    <?import java.lang.*?>
    <?import javafx.scene.layout.*?>
    
    
    <StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
       <children>
          <VBox alignment="CENTER" maxHeight="-Infinity" maxWidth="-Infinity">
             <children>
                <Label text="Label" />
                <HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity">
                   <children>
                      <Button mnemonicParsing="false" text="Button" />
                      <Button mnemonicParsing="false" text="Button" />
                   </children>
                </HBox>
             </children>
          </VBox>
       </children>
    </StackPane>
    

相关问题