Javafx Tile Pane,设置最大列数 [英] Javafx Tile Pane, set max number of columns

查看:129
本文介绍了Javafx Tile Pane,设置最大列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我将解释我的目标.我想渲染一个这样的表:

First of all, i will explain my objetive. I want render a table like this:

每个单元格的内容均在执行时间中确定,但其大小固定为13x13.

The content of every cell is determined in Execution Time, but his size is fixed to 13x13.

因此,我的方法是创建一个图块窗格,将列数设置为13并创建单元格.

So, my approach is create a tile pane, set the numbers of columns to 13 and create the cells.

pane = new TilePane();
pane.setPadding(new Insets(10, 10, 10, 10));
pane.setVgap(5);
pane.setHgap(5);
pane.setPrefColumns(13);

这有效:

但是当我调整窗口大小时:

But when i resize the window:

现在有13列更多!

我的测试代码:

package sample;

import javafx.application.Application;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            TilePane pane = new TilePane();
            pane.setPadding(new Insets(10, 10, 10, 10));
            pane.setVgap(5);
            pane.setHgap(5);
            pane.setPrefColumns(13);
            pane.setMaxWidth(Region.USE_PREF_SIZE);
            ObservableList<Node> list = pane.getChildren();

            for (int i = 0;i < 200;i++){
                Label view = new Label();
                view.setText(""+(i+1));
                list.add(view);
            }

            primaryStage.setScene(new Scene(pane,400,400));
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

你有什么主意吗?

推荐答案

设置最大宽度以使用首选宽度是正确的方法.示例中代码的唯一问题是,您已将平铺窗格设为场景的根.根节点的大小将始终为场景的大小,忽略最小,最大和首选大小. (如果您没有为场景指定大小,那么将根据根节点的首选大小调整场景的大小,但仍然允许根节点任意增大或缩小.)

Setting the max width to use the preferred width is the correct approach. The only problem with the code in your example is that you have made the tile pane the root of the scene. The root node will always be sized to the size of the scene, ignoring the minimum, maximum, and preferred size. (If you don't specify a size for the scene, then the scene is sized according to the preferred size of the root node, but the root node is still allowed to grow or shrink arbitrarily.)

因此,如果您仅将磁贴窗格包装在另一个窗格中,它将按预期工作:

So if you simply wrap the tile pane in another pane, it will work as expected:

primaryStage.setScene(new Scene(new StackPane(pane)));

这篇关于Javafx Tile Pane,设置最大列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆