【问题标题】:JavaFX TilePane setPrefColumn/Row methods not working?JavaFX TilePane setPrefColumn/Row 方法不起作用?
【发布时间】:2015-12-23 04:57:18
【问题描述】:

我正在尝试在 JavaFX 中制作 Suduko 板。我听说 TilePane 对此特别有用,因为 TilePane 背后的整个想法是每个 'Tile' 的大小都是统一的。太好了,这正是 Suduko 板、棋盘、跳棋、井字游戏、战舰等的方式。听起来 TilePane 是任何棋盘游戏应用程序的必备窗格。

是吗?

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextFormatter.Change;
import javafx.scene.image.Image;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SudukoSolver extends Application
{
    Stage window;
    Scene scene;
    private final int TEXTFIELD_WIDTH = 32;
    private final int TEXTFIELD_HEIGHT = 32;

    @Override public void start(Stage window) throws Exception
    {       
        this.window = window;
        window.setTitle("Suduko Solver");
        window.setOnCloseRequest(e -> closeProgram());

        // Does setting this to false defeat the purpose of TilePane?
        window.setResizable(false); 

        VBox root = new VBox(); 
        //root.setAlignment(Pos.CENTER);

        TilePane tiles = new TilePane();    
        tiles.setAlignment(Pos.CENTER);

        // Does not appear to do anything.
        tiles.setPrefColumns(9);
        tiles.setPrefRows(9);

        // Add all the tiles to the Pane.
        root.getChildren().add(tiles);

        for (int i = 0; i < 81; i++)
        {
            TextField textBox = new TextField();
            textBox.setMinHeight(TEXTFIELD_HEIGHT);
            textBox.setMaxHeight(TEXTFIELD_HEIGHT);
            textBox.setMinWidth(TEXTFIELD_WIDTH);
            textBox.setMaxWidth(TEXTFIELD_WIDTH);

            textBox.setTextFormatter(new TextFormatter<String>((Change change) -> 
            {
                String newText = change.getControlNewText();
                if (newText.length() > 1) 
                {
                    return null ;
                }
                else if (newText.matches("[^1-9]"))
                {
                    return null;
                }
                else 
                {
                    return change ;
                }
            }));

            tiles.getChildren().add(textBox);
        }


        scene = new Scene(root, 600, 750);
        window.setScene(scene);
        window.show();
    }

    /**
     * This method is called when the user wishes to close the program.
     */
    private void closeProgram()
    {
        Platform.exit();
    }

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

请注意这不是 9x9 网格。

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    在您的代码中,TilePane 的宽度由父 VBox 而非 TilePaneprefColumns 属性决定。

    来自javadoc of prefColumns

    此值仅用于计算 tilepane 的首选大小,可能无法反映 rows 列的实际数量,如果将 tilepane 的大小调整为首选 高度宽度。

    (我修复了文档中的一些错误。)

    您需要使用不会调整TilePane 大小的父级。 (VBox 默认调整其子项的大小。)使用VBox.setFillWidth 更改此行为:

    root.setFillWidth(false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多