- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 JavaFX 创建一个场景,其中程序的标题位于顶部中心,按钮位于场景左侧的垂直线上。然而,这两个元素都聚集在场景的右上角,而不是我想要的位置。
如何让这些元素显示在我想要的位置?
以下是我尝试设置程序标题位置的方法:
grid.add(gameTitle, 0, 0);
GridPane.setHalignment(gameTitle, HPos.CENTER);
GridPane.setValignment(gameTitle, VPos.TOP);
我尝试类似地设置 VBox 对象:
grid.getChildren().add(buttonBox);
GridPane.setHalignment(buttonBox, HPos.LEFT);
GridPane.setValignment(buttonBox, VPos.CENTER);
我的整个 MainMenu 类。 (这个类在我的主类中被调用来构建场景):
package scenes;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.application.Platform;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class MainMenu {
public Pane getMainMenuPane() {
// Create the scene grid
GridPane grid = new GridPane();
grid.setHgap(10);
grid.setVgap(10);
// Set the game title to the top center
Text gameTitle = new Text("Bandit King");
Font titleFont = new Font(75);
gameTitle.setFont(titleFont);
//
grid.add(gameTitle, 0, 0);
GridPane.setHalignment(gameTitle, HPos.CENTER);
GridPane.setValignment(gameTitle, VPos.TOP);
// Create Button objects and put in VBox
Button[] buttArr = makeButtons();
VBox buttonBox = new VBox();
buttonBox.getChildren().addAll(buttArr);
buttonBox.setSpacing(10);
// add Button VBox to GridPane
grid.getChildren().add(buttonBox);
GridPane.setHalignment(buttonBox, HPos.LEFT);
GridPane.setValignment(buttonBox, VPos.CENTER);
return (Pane) grid;
}
private Button[] makeButtons() {
// Create buttons
Button start = new Button("Start a New Game");
Button load = new Button("Load a Saved Game");
Button exit = new Button("Exit the Game");
// set Button actions
start.setOnAction( a -> {
System.out.println("WIP- start game.");
});
load.setOnAction( a -> {
System.out.println("WIP- load game");
});
exit.setOnAction( a -> {
Platform.exit();
System.exit(0);
});
// return Button[] array
Button[] buttArr = {start, load, exit};
return buttArr;
}
}
我的主类(显示场景):
package central;
import javafx.stage.Stage;
import scenes.*;
import controllers.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
public class Main extends Application {
// Get scene panes
private static Pane mainMenu = new MainMenu().getMainMenuPane();
// Create SceneController object.
private static Scene scene = new Scene(mainMenu, 1600, 900);
public static SceneController SceneControl = new SceneController(scene);
@Override
public void start(Stage stage) {
stage.setTitle("Bandit King");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
最佳答案
您添加 GridPane
子级的默认单元格是 (0, 0)
,这就是您在这一行中所做的操作:
grid.getChildren().add(buttonBox);
您需要将其更改为
grid.add(buttonBox, 0, 1);
将行索引设置为 1。(还有其他方法可以以这种方式分配行索引,但在这种情况下这是最方便的选项。)
但这不会导致第一列占据 GridPane
的整个宽度。如果您还希望第一列占据所有可用宽度,则需要通过添加 ColumnConstraints
来指定这一点:
ColumnConstraints constraints = new ColumnConstraints();
constraints.setHgrow(Priority.ALWAYS);
grid.getColumnConstraints().add(constraints);
关于JavaFX GridPane 对象对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53253552/
我正在阅读一些有关 JavaFX 2.0 的教程,我对 GridPane 布局组件的两种方法有点困惑。 基本上,我会使用 GridPane.add(new Label("My Label"), 0,
我正在使用 JavaFX 8 创建一个应用程序。我使用拖/放动态更改网格 Pane 的内容。我希望每行或每行/列迭代 GridPane 内容。JavaFX 允许通过指定行和列在 GridPane 中添
我想检索网格 Pane 中一个特定单元格的内容。我在单元格中放置了按钮 setConstraints(btt , 0 ,1 ) setConstraints(btt , 0 ,2 ) getChild
基本上,我只想将图像插入网格 Pane 内的单元格中。 GridPane gridpane = new GridPane(); gridpane.add(new Image("File:image/m
我已经使用了GridPane.getChildren().clear(),但是它不起作用。 最佳答案 请注意,gridLinesVisible 属性是一项调试功能,不应依赖它来创建特定的外观。 话虽这
所以我有一个登录页面。如果您点击“还不是用户?立即注册!”它应该在同一窗口中打开一个新页面。现在我正在打开第二页,但它位于单独的窗口中。它还会在打开注册页面时创建错误消息。这是代码: 主要 publi
我正在使用数字键盘制作登录屏幕,但我似乎无法将 Pane 中按钮的网格 Pane 居中对齐。我做错了什么? Main.java import javafx.application.Applicatio
我正在尝试使用 JavaFX 创建一个场景,其中程序的标题位于顶部中心,按钮位于场景左侧的垂直线上。然而,这两个元素都聚集在场景的右上角,而不是我想要的位置。 如何让这些元素显示在我想要的位置? 以下
我对 JavaFX 比较陌生,并且我一直很困惑为什么下面的代码没有产生添加到网格的标签的预期结果。 我想做的是运行一个测试,将 JavaFX 标签添加到我的 FXML GridPane 中,因为我想在
作为一项作业,我正在 JavaFX 中制作一个老虎机程序。 如果我这样做: GridPane grid = new GridPane(); grid.add(new Rectangle(20, 20)
我正在尝试使用 2 列创建一个具有特定布局的窗口,但我无法完全让它按照我想要的方式工作。 首先,一些简化的示例代码: import javafx.application.Application; im
我是 JavaFX 新手,目前我尝试构建一个小型聊天窗口。为此,我为 ListView 设置了以下自定义单元格布局。 Grid 我使用外部 HBox,因此我可以像 Whatsapp... 那样对齐消息
我正在从数据库加载数据并在 GridPane 中动态创建按钮!但数据量很大时,要花很长时间才能看到按钮!我正在使用线程从数据库加载数据,但似乎创建每个按钮并填充 GridPane 花费的时间太长! *
您好,我目前正在使用 JavaFX 中的 GridPane,偶然发现了一个问题...我想创建一个包含三行的布局,其中中间一行会增长并占用所有可用空间,但我无法得到它工作。中间的行变得太大,并将窗口下方
对于 GUI,我必须将多个按钮设置为 GridPane。为此,我想问一下是否有一种方法可以比我更优雅地在网格中添加多个按钮。这是我的代码的摘录。 Button button0 =new Button(
我正在编写一个生成随机颜色网格的 JavaFX 程序。调整窗口大小时,网格应调整为尽可能大,同时仍保持正方形并在底部为文本留出空间。 一切正常,但我遇到的问题是调整大小时 GridPane 中留下的间
我正在使用 JavaFX 8。尝试使用 GridPage 来显示一组属性和值。有些值可能太长,无法放在一行中。我想包装这些值,但似乎无法弄清楚如何使网格 Pane 垂直扩展以容纳包装的文本。我基本上想
我正在尝试使用 GridPane 创建登录 UI。我的窗口以 300 x 300 打开,并且我已将行和列索引设置为某些值,以便我的文本“用户 ID”和“密码”出现在窗口的中心(与所有四个边的距离相等)
我有这个构造函数,我正在尝试用标签填充网格 Pane 。我碰壁了,不知道出了什么问题。我需要在 1 行中创建 13 个标签。 构造函数: public class Labels { @FXML
我已经在我的游戏中实现了拖放功能,但到目前为止我只能“拖放”到硬编码位置。如图所示: 我想要的是: 当飞船被放下时,它的 x,y 值(相对于 GridPane)被保存,或者 保存飞船掉落的单元格。 我
我是一名优秀的程序员,十分优秀!