Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

java - setText to many buttons - javaFX

Im working on some card game in java. I'm using javaFX for an user inteface but i have a little problem with it. I have 25 buttons and i would like to add text to all of them. I have done it but the code is ugly and very long. I'm using scenebuilder and I created button there, every button has uniqe id from 1 to 25. Is there any way to shorten this code?

   @FXML
   Button card1;
   @FXML
   Button card2;
   @FXML
   Button card3;
   @FXML
   Button card4;
   @FXML
   Button card5;
   @FXML
   Button card6;
   @FXML
   Button card7;
   @FXML
   Button card8;
   @FXML
   Button card9;
   @FXML
   Button card10;
   @FXML
   Button card11;
   @FXML
   Button card12;
   @FXML
   Button card13;
   @FXML
   Button card14;
   @FXML
   Button card15;
   @FXML
   Button card16;
   @FXML
   Button card17;
   @FXML
   Button card18;
   @FXML
   Button card19;
   @FXML
   Button card20;
   @FXML
   Button card21;
   @FXML
   Button card22;
   @FXML
   Button card23;
   @FXML
   Button card24;
   @FXML
   Button card25;

   @FXML
   public void initialize(){
       Board board = new Board();
       board.createBoard();
       card1.setText(board.getListFields().get(0).getWord().name());
       card2.setText(board.getListFields().get(1).getWord().name());
       card3.setText(board.getListFields().get(2).getWord().name());
       card4.setText(board.getListFields().get(3).getWord().name());
       card5.setText(board.getListFields().get(4).getWord().name());
       card6.setText(board.getListFields().get(5).getWord().name());
       card7.setText(board.getListFields().get(6).getWord().name());
       card8.setText(board.getListFields().get(7).getWord().name());
       card9.setText(board.getListFields().get(8).getWord().name());
       card10.setText(board.getListFields().get(9).getWord().name());
       card11.setText(board.getListFields().get(10).getWord().name());
       card12.setText(board.getListFields().get(11).getWord().name());
       card13.setText(board.getListFields().get(12).getWord().name());
       card14.setText(board.getListFields().get(13).getWord().name());
       card15.setText(board.getListFields().get(14).getWord().name());
       card16.setText(board.getListFields().get(15).getWord().name());
       card17.setText(board.getListFields().get(16).getWord().name());
       card18.setText(board.getListFields().get(17).getWord().name());
       card19.setText(board.getListFields().get(18).getWord().name());
       card20.setText(board.getListFields().get(19).getWord().name());
       card21.setText(board.getListFields().get(20).getWord().name());
       card22.setText(board.getListFields().get(21).getWord().name());
       card23.setText(board.getListFields().get(22).getWord().name());
       card24.setText(board.getListFields().get(23).getWord().name());
       card25.setText(board.getListFields().get(24).getWord().name());

   }```

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something like is what everyone means when they say you should create those items in java and not in fxml

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        GridPane gridPane = new GridPane();

        //This is so you can access them later
        ArrayList<Button> buttonList = new ArrayList<>();

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5; j++) {
                Button button = new Button();
                button.setText("Card:"+i*j);
                button.setPrefSize(80,120);
                //If you need to know what card it is add the below line
                button.setId(String.valueOf(i*j));

                buttonList.add(button);

                gridPane.add(button, i, j);
            }
        }

        Scene scene = new Scene(gridPane);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...