如何锁定TilePane按钮

时间:2019-03-01 10:50:03

标签: java javafx

我正在尝试使用javaFx创建小型电话GUI。

我的教授建议我使用tilePane。

我目前有12个成功显示的按钮,但是与电话不同,它们没有锁定在3x4网格中。

无论窗口大小如何,按钮应如何设置:

enter image description here

但是它们的外观...

enter image description here

我将如何停止呢?

public void start(Stage stage) throws Exception
{
    // Create a Button or any control item
    Button Button1 = new Button("1");
    Button Button2 = new Button("ABC\n  2");
    Button Button3 = new Button("DEF\n  3");
    Button Button4 = new Button("GHI\n  4");
    Button Button5 = new Button("JKL\n  5");
    Button Button6 = new Button("MNO\n  6");
    Button Button7 = new Button("PQRS\n  7");
    Button Button8 = new Button("TUV\n  8");
    Button Button9 = new Button("WXYZ\n  9");
    Button ButtonStar = new Button("*");
    Button Button0 = new Button("0");
    Button ButtonPlus = new Button("+");

    // Create a new grid pane
    TilePane pane = new TilePane();

    pane.setPrefColumns(3);
    pane.setPrefRows(4);

    pane.setPadding(new Insets(10, 10, 10, 10));
    pane.setMinSize(300, 300);
    pane.setVgap(10);
    pane.setHgap(10);

    //set an action on the button using method reference
    Button1.setOnAction(this::buttonClick);
    Button2.setOnAction(this::buttonClick);
    Button3.setOnAction(this::buttonClick);
    Button4.setOnAction(this::buttonClick);
    Button5.setOnAction(this::buttonClick);
    Button6.setOnAction(this::buttonClick);
    Button7.setOnAction(this::buttonClick);
    Button8.setOnAction(this::buttonClick);
    Button9.setOnAction(this::buttonClick);
    ButtonStar.setOnAction(this::buttonClick);
    Button0.setOnAction(this::buttonClick);
    ButtonPlus.setOnAction(this::buttonClick);

    // Add the button and label into the pane

    pane.getChildren().add(Button1);
    pane.getChildren().add(Button2);
    pane.getChildren().add(Button3);
    pane.getChildren().add(Button4);
    pane.getChildren().add(Button5);
    pane.getChildren().add(Button6);
    pane.getChildren().add(Button7);
    pane.getChildren().add(Button8);
    pane.getChildren().add(Button9);
    pane.getChildren().add(ButtonStar);
    pane.getChildren().add(Button0);
    pane.getChildren().add(ButtonPlus);

    // JavaFX must have a Scene (window content) inside a Stage (window)
    Scene scene = new Scene(pane, 300,100);
    stage.setTitle("Phone");
    stage.setScene(scene);

    scene.getStylesheets().add("ButtonLook.css");

    // Show the Stage (window)
    stage.show();      
}

0 个答案:

没有答案