JavaFX TilePane setPrefColumn / Row方法不起作用?

时间:2015-12-23 04:57:18

标签: java user-interface javafx

我正在尝试在JavaFX中制作一个Suduko板。我听说TilePane特别适合这个,因为TilePane背后的整个想法是每个'Tile'都是统一的。太棒了,这就是Suduko板,棋盘,跳棋,Tic Tac Toe,战舰等等。听起来像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网格。

enter image description here

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:4)

在您的代码中,TilePane的宽度由父VBox而不是prefColumns的{​​{1}}属性确定。

来自javadoc of prefColumns

  

此值仅用于计算tilepane的首选大小,并且可能不会反映 rows 列的实际数量,如果将tilepane调整为其首选之外的其他值,则可能会更改这些值>身高宽度。

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

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

VBox