最终和有效最终变量上的JavaFX EventHandling [英] JavaFX EventHandling on final and effectively final variables

查看:62
本文介绍了最终和有效最终变量上的JavaFX EventHandling的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码段的一部分:

for (int col=1;col<=7;col++) {
    for (int row=2;row<=31;row++) {
        if (col!=4) {
            b = new Button();
            b.setText(Seat.calcID(row, col));
            b.setMinSize(2, 2);
            b.setFont(new Font(8));
            addableSeat.change(x, row, col);
            System.out.println(row+" "+col);
            b.setOnAction(e->{
                book.addSeat(Seat.genSeat(x, row, col));
                System.out.println(">>>"+addableSeat.accessId()+"<<<");
            });
            no=(col>4)?col+1:col;
            grid.setConstraints(b,no,row);
            grid.getChildren().add(b);
        }
    }
}

存在一个错误,指出事件处理程序中的变量"row"和"col"必须是最终的或实际上是最终的.我该如何实现?

There is an error which states that the variables "row" and "col" within the eventhandler must be final or effectively final. How can I achieve that?

如果您需要其他代码段,我愿意提供.谢谢!

If you need additional code snippets I am willing to provide. Thanks!

推荐答案

在大多数情况下,采用 Jai 提出的简单通用解决方案a>.
在某些情况下,您可能希望将rowcol声明为类变量(字段):

In most cases adopt the simple common solution proposed by Jai.
In some cases you may prefer to declare row and col as class variables (fields):

private int row, col; 

    for (col=1;col<=7;col++) {
        for (row=2;row<=31;row++) {
            if (col!=4) {
                ...
                ...
                b.setOnAction(e->{
                    book.addSeat(Seat.genSeat(x, row, col));
                 });
            }
        }
    }

这篇关于最终和有效最终变量上的JavaFX EventHandling的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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