【问题标题】:JavaFX center two buttons below each otherJavaFX将两个按钮居中
【发布时间】:2015-08-26 20:57:53
【问题描述】:

我是 JavaFX 的新手。有谁知道我如何在屏幕中间垂直放置两个按钮(彼此下方)。

这是我目前的代码:

public class StartMenu {
private StackPane gridStartMenu;
private Button createNewTournamentbtn;
private Button viewTournamentbtn;
private Scene startMenuScene;
private VBox vboxStartMenu;

public StartMenu() {
    gridStartMenu = new StackPane();
    vboxStartMenu = new VBox();

    //BUTTON 1
    createNewTournamentbtn = new Button("Create new tournament");
    createNewTournamentbtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
    createNewTournamentbtn.setPrefSize(300, 50);

    //BUTTON 2
    viewTournamentbtn = new Button("View tournaments");
    viewTournamentbtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
    viewTournamentbtn.setPrefSize(300, 50);

    gridStartMenu.setAlignment(createNewTournamentbtn, Pos.CENTER);
    gridStartMenu.setAlignment(viewTournamentbtn, Pos.CENTER);


    gridStartMenu.getChildren().addAll(createNewTournamentbtn,
            viewTournamentbtn);

    startMenuScene = new Scene(gridStartMenu, 600, 600);
    MainApp.getWindow().setScene(startMenuScene);
    MainApp.getWindow().show();

【问题讨论】:

  • 你用过JavaFX Scenebuilder吗?
  • 没有没用过Scenebuilder
  • 如果您使用的是 Eclipse,您可能会考虑使用它。它将允许您拖放按钮、下拉菜单等。
  • OK 听起来不错。我会看看它。谢谢。
  • 即使您使用诸如 SceneBuilder 之类的快速应用程序开发 (RAD) 工具,您仍然需要了解它可以帮助您创建的布局窗格。请参阅tutorial

标签: button javafx


【解决方案1】:

一个善意的建议:不要过度依赖 Scenebuilder 或类似的东西。如果你真的想学习 JavaFX,完全自己编写代码。

这就是你可以做你想做的事的方法:

首先,在VBox中添加两个按钮:

VBox vbox = new VBox(5); // 5 is the spacing between elements in the VBox
vbox.getChildren().addAll(new Button("Button 1"), new Button("Button 2"));

然后,使用以下代码将 VBox 居中:

stackPane.getChildren().add(vbox);
StackPane.setAlignment(vbox, Pos.CENTER);
stage.setScene(new Scene(stackPane));
stage.show();

【讨论】:

  • 非常感谢!有效:)。也感谢您的建议。我真的很感激。
【解决方案2】:

首先,我想提一下 FXML 的一些优点:

查看链接Why use FXML

使用 FXML 实现:

<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<StackPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox alignment="CENTER" prefHeight="200.0" prefWidth="100.0" spacing="30.0">
         <children>
            <Button mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" style="-fx-base: #b6e7c9;;" text="Create new tournament">
               <font>
                  <Font name="Arial" size="22.0" />
               </font>
            </Button>
            <Button mnemonicParsing="false" prefHeight="50.0" prefWidth="300.0" style="-fx-base: #b6e7c9;" text="View tournaments">
               <font>
                  <Font name="Arial" size="22.0" />
               </font>
            </Button>
         </children>
      </VBox>
   </children>
</StackPane>

snapshot

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2018-02-23
    • 2012-02-04
    • 2013-08-07
    • 2020-03-14
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多