javafx 将按钮添加到网格 Pane

标签 java javafx javafx-2 javafx-8

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

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);

    }

}

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

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

最佳答案

您没有在按钮中定义它,您总是使用非 final 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);
    }
}

如果你想使用 lambda 表达式:

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

关于javafx 将按钮添加到网格 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24715911/

相关文章:

java - 获取动态添加的按钮的事件

javafx - 如何确定JavaFX应用程序所需的FXML文件,CSS文件,图像和其他资源的正确路径?

javafx-2 - 如何在JAVAFX中使用带有动态列的动态TableView编辑数据

javafx - 输入键事件在 Javafx 对话框中不起作用?

javafx - 如何在运行 5 秒后更新 JavaFX 应用程序中的文本框?

Netbeans 中的 Java FX 编辑器

java - 提高核心 Java 生产力的前 3 个库是什么?

java - 如何在无 Activity 类中测试 Intent

java - 在 Eclipse RCP 中使用 jfreechart 创建的条形图上不显示文本字段

java - 什么是 NullPointerException,我该如何解决?