javafx将按钮添加到网格窗格 [英] javafx adding button to grid pane

查看:135
本文介绍了javafx将按钮添加到网格窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 .

推荐答案

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

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:

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表达式:

And if you want to use lambda expressions:

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

这篇关于javafx将按钮添加到网格窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆