如何设置多个按钮并将它们添加到gridPane

时间:2016-06-09 12:52:58

标签: java user-interface javafx gridpane

对于GUI我必须将多个按钮设置为GridPane。为此,我想问一下是否有办法以比我更优雅的方式在网格中添加多个按钮。 这是我的代码的摘录。

Button button0 =new Button("0");
    Button button1 =new Button("1");
    Button button2 =new Button("2");
    Button button3 =new Button("3");
    Button button4 =new Button("4");
    Button button5 =new Button("5");
    Button button6 =new Button("6");
    Button button7 =new Button("7");
    Button button8 =new Button("8");
    Button button9 =new Button("9");
    Button cancel = new Button("C");
    Button plus = new Button("+");
    Button minus = new Button("-");
    Button multiplicate = new Button("*");
    Button divide = new Button("/");
    Button equal = new Button("=");

对于所有这些按钮,我必须分别设置行和列索引。

root.setPadding(new Insets(5,5,5,5));
    GridPane.setConstraints(root, 4, 3);
    root.getChildren();
    root.add(label, 1, 1, 3, 1);
    root.add(button1, 2, 2);
    root.add(button2, 3, 2);
    root.add(button3, 4, 2);
    root.add(button4, 2, 3);
    root.add(button5, 3, 3);
    root.add(button6, 4, 3);
    root.add(button7, 2, 4);
    root.add(button8, 3, 4);
    root.add(button9, 4, 4);
    root.add(button0, 2, 5);
    root.add(cancel, 3, 5);
    root.add(plus, 5,2);
    root.add(minus, 5,3);
    root.add(multiplicate, 5, 4);
    root.add(divide, 5,5);
    root.add(equal, 4, 5);

我对任何建议都非常有帮助。

2 个答案:

答案 0 :(得分:1)

您应该使用评论中建议的FXML文件。

但是如果你想坚持使用Java代码,你可以做这样的事情( 我的建议仍然有一个FXML文件 ):

Main.java

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            GridPane root = new GridPane();
            Scene scene = new Scene(root, 400, 400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            Button b = new Button();
            b.setOnAction(new EventHandler<ActionEvent>() {

                @Override
                public void handle(ActionEvent event) {
                }
            });

            GridHelper.addToGrid(root, new Button("0"), 2, 5);
            GridHelper.addToGrid(root, new Button("1"), 2, 2);
            GridHelper.addToGrid(root, new Button("2"), 3, 2);
            GridHelper.addToGrid(root, new Button("3"), 4, 2);
            GridHelper.addToGrid(root, new Button("4"), 2, 3);
            GridHelper.addToGrid(root, new Button("5"), 3, 3);
            GridHelper.addToGrid(root, new Button("6"), 4, 3);
            GridHelper.addToGrid(root, new Button("7"), 2, 4);
            GridHelper.addToGrid(root, new Button("8"), 3, 4);
            GridHelper.addToGrid(root, new Button("9"), 4, 4);

            GridHelper.addToGrid(root, new Button("C"), 3, 5, e -> System.out.println("C"));
            GridHelper.addToGrid(root, new Button("+"), 5, 2, e -> System.out.println("+"));
            GridHelper.addToGrid(root, new Button("-"), 5, 3);
            GridHelper.addToGrid(root, new Button("*"), 5, 4);
            GridHelper.addToGrid(root, new Button("/"), 5, 5);
            GridHelper.addToGrid(root, new Button("="), 4, 5);

            root.setPadding(new Insets(5, 5, 5, 5));

            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class GridHelper {

        static void addToGrid(GridPane grid, Node c, int col, int r) {
            grid.add(c, col, r);
        }

        static void addToGrid(GridPane grid, Node c, int col, int r, EventHandler<ActionEvent> event) {
            grid.add(c, col, r);
            addButtonEvent(c, event);
        }

        static void addToGrid(GridPane grid, Node c, int col, int r, int cs, int rs) {
            grid.add(c, col, r, cs, rs);
        }

        static void addToGrid(GridPane grid, Node c, int col, int r, int cs, int rs, EventHandler<ActionEvent> event) {
            grid.add(c, col, r, cs, rs);
            addButtonEvent(c, event);
        }

        static void addButtonEvent(Node button, EventHandler<ActionEvent> event) {
            if (button instanceof Button)
                ((Button) button).setOnAction(event);
        }

    }

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

它只是简单地介绍了一个静态类,它暴露了一些静态方法,将Node添加到GridPane到指定位置,并可选择设置跨度(row / colum)。如果是Button,您也可以添加新闻事件。当然可以进一步概括,它只是一个确切问题的样本。

你也可以为Button 0 - 9制作一个循环(正如评论中提出的那样)。

答案 1 :(得分:0)

public class MainController implements Initializable {

    int z1=0,y2=0,x=0; 

    @FXML
    GridPane gp;

    public void initialize(URL arg0, ResourceBundle arg1) {
        // TODO Auto-generated method stub
        while(z1<4){    //add 4 buttons
            addButton();
            z1++;
        }   
    }   

    private void addButton() {
        // TODO Auto-generated method stub
        final Button temp1=new Button("Button "+ y2);
        final int numberButton=y2;
        temp1.setId(""+y2);
        temp1.setText("whatever u want!!");
        temp1.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                // TODO Auto-generated method stub
                System.out.println("id("+temp1.getId()+")="+numberButton);
            }
        });
        gp.add(temp1, x, 0);  //x is column index and 0 is row index
        x++;
    }
}