- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我在搞javafx
的时候,我遇到过这样的事情:
我创建了一个 GridPane
,将其包装在 ScrollPane
中并填充 Buttons
,但更改了 RowConstraints
和 事先为
。GridPane
设置 ColumnConstraints
问题是水平滚动条看起来不够充分。我相信它是一个如此庞大的 GridPane
,但它是如何发生的呢?
然而垂直滚动条完全没问题。
sample.fxml
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SampleController">
<children>
<ScrollPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<content>
<GridPane fx:id="gridPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
</GridPane>
</content>
</ScrollPane>
</children>
</AnchorPane>
sampleController.java
public class SampleController {
@FXML public GridPane gridPane;
@FXML
public void initialize(){
RowConstraints rowConstraints = new RowConstraints(10.0, 40, 40);
ColumnConstraints columnConstraints = new ColumnConstraints(10.0, 40, 40);
int i=0,j=0;
for(i=0; i<50; i++){
gridPane.getRowConstraints().add(i, rowConstraints);
for(j=0; j<30; j++) {
gridPane.getColumnConstraints().add(j, columnConstraints);
Button btn = new Button(Integer.toString(j+1));
btn.setPrefSize(40, 40);
gridPane.add(btn, j, i);
}
}
}
}
Dashboard.java
public class sample extends Application{
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Scene scene = new Scene(root, 300, 275);
stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();
}
}
最佳答案
您要为每一行添加列列约束,这意味着最终您将拥有 50 * 30 = 1500
约束,而不是 30
。您需要在行循环外部的循环中添加列约束。此外,假设在添加约束之前约束列表为空,则无需指定要插入的索引,因为 add
也会插入到 List
的末尾。请检查您是否确实需要在 fxml 中创建的列约束,因为代码变得更简单,如果您不需要将列约束插入列表的倒数第二个元素:
for (int i = 0; i < 30; i++) {
gridPane.getColumnConstraints().add(columnConstraints);
}
for(int i = 0; i < 50; i++){
gridPane.getRowConstraints().add(rowConstraints);
for(int j = 0; j < 30; j++) {
Button btn = new Button(Integer.toString(j + 1));
btn.setPrefSize(40, 40);
gridPane.add(btn, j, i);
}
}
顺便说一句:请注意,AnchorPane
约束对非 AnchorPane
子节点的节点没有任何影响;您可以安全地从 fxml 中的 GridPane 中删除这些约束。
关于ScrollPane 中的 JavaFX GridPane 比预期大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47233711/
我正在阅读一些有关 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)被保存,或者 保存飞船掉落的单元格。 我
我是一名优秀的程序员,十分优秀!