【发布时间】: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