javafx 将按钮添加到网格窗格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24715911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:24:03  来源:igfitidea点击:

javafx adding button to grid pane

javajavafxjavafx-2javafx-8

提问by user3829658

I am adding button to grid pane dynamically but after giving them function they all show same function and i don't knows why?

我正在动态地向网格窗格添加按钮,但是在给它们功能后,它们都显示相同的功能,我不知道为什么?

import java.awt.Panel;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;

public class TestController implements Initializable {
    @FXML
    Panel mpanel;
    @FXML 
     GridPane gpnael;
int x=0,y=0,i=0,y1=0;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    @FXML
    private void add(ActionEvent event) {
        y1++;
        Button  temp = new Button("Button " + i);
                temp.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent e) {
                        System.out.println("button"+y1);
                    }
                });

                gpnael.add(temp,i++,1);

    }

}

now i have added three button to grid pane when i click on each button they show same output.

现在我在网格窗格中添加了三个按钮,当我点击每个按钮时,它们显示相同的输出。

I want that they all show different output as assigned .

我希望它们都显示不同的输出分配。

采纳答案by Mansueli

You are not defining it in the buttons, you are always using a non final int to express your values you should try do make them with unique values or to set an id for each button and get the value from the id:

您没有在按钮中定义它,您总是使用非最终 int 来表达您的值,您应该尝试使用唯一值或为每个按钮设置一个 id 并从 id 中获取值:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class ButtonsOnGPanel extends Application {

    private int i = 0;
    private GridPane gpnael = new GridPane();
    @Override
    public void start(Stage stage) throws Exception {
        Pane root = new Pane();
        while(i<3){
            addButton();
        }
        root.getChildren().add(gpnael);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
    private void addButton() {
        i++;
        final Button temp = new Button("Button " + i);
        final int numButton= i;
        temp.setId("" + i);
        temp.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                System.out.println("id(" + temp.getId()  + ") =  " + numButton);
            }
        });
        gpnael.add(temp, i, 1);
    }
    public static void main(String[] args) {
        launch(args);
    }
}

And if you want to use lambda expressions:

如果你想使用 lambda 表达式:

    temp.setOnAction((ActionEvent e) -> {
        System.out.println("id(" + temp.getId()  + ") =  " + numButton);
    });