这感觉就像我在作弊或做错了什么。我是一名 Java 学生,正在开发一个简单的 JavaFX 项目。
当我在 flowPane 中循环并创建按钮时,我在内部类中使用循环计数器 i 时遇到了问题。这是我分配事件处理程序的部分。我以前处理过这个问题,我明白“最终”和“有效最终”之间的区别,所以我不相信我在问这个。
只是使用“int thisI = i”创建 i 的副本在设计上感觉是错误的。难道没有更好的方法吗?我研究了 lambda,它们也有“最终或有效最终”的要求。
这是我的代码,欢迎任何批评或改进建议,谢谢!
private FlowPane addFlowPaneCenter() {
FlowPane flow = new FlowPane();
flow.setPadding(new Insets(0, 0, 0, 0));
flow.setVgap(0);
flow.setHgap(0);
flow.setPrefWrapLength(WIDTH_OF_CENTER); // width of function buttons
Button centerButtons[] = new Button[NUM_BUTTONS];
ImageView centerImages[] = new ImageView[NUM_BUTTONS];
for (int i=0; i < NUM_BUTTONS; i++) {
centerImages[i] = new ImageView(
new Image(Calculator.class.getResourceAsStream(
"images/button-"+(i)+".png")));
centerButtons[i] = new Button();
centerButtons[i].setGraphic(centerImages[i]);
centerButtons[i].setPadding(Insets.EMPTY);
centerButtons[i].setId("button-"+(i));
flow.getChildren().add(centerButtons[i]);
// add a drop shadow on mouseenter
DropShadow shadow = new DropShadow();
// ***** here's the workaround is this really a good approach
// to use this in the inner class instead of i? thanks *****
int thisI = i;
// set event handlers for click, mousein, mouseout
centerButtons[i].setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
// change graphic of button to down graphic
ImageView downImage = new ImageView(new
Image(Calculator.class.getResourceAsStream(
"images/button-"+(thisI)+"D.png")));
// call function to effect button press
System.out.println("Button click");
// change graphic back
centerButtons[thisI].setGraphic(centerImages[thisI]);
}});
centerButtons[i].addEventHandler(MouseEvent.MOUSE_ENTERED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
centerButtons[thisI].setEffect(shadow);
}
});
centerButtons[i].addEventHandler(MouseEvent.MOUSE_EXITED,
new EventHandler<MouseEvent>() {
@Override public void handle(MouseEvent e) {
centerButtons[thisI].setEffect(null);
}
});
}
return flow;
}
最佳答案
您可以完全删除数组 centerButtons 和 centerImages。相反,在循环中为图像和按钮创建局部变量并使用它们,例如
final ImageView image = new ImageView(...);
final Button button = new Button();
button.setGraphic(centerImages[i]);
...
您可以在事件处理程序中使用局部变量,例如
button.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
...
// change graphic back
button.setGraphic(image);
}});
我注意到的两个小改进:
- 尽量避免多次创建 Image,因为每次创建 Image 时,实际数据都会被重新加载。您的处理程序将为每次点击创建一个新图像。我通常在静态最终字段中创建图像。
- 事件处理程序是练习 lambda 表达式的好机会。 :)
关于Java/JavaFX 绕过 Final 或有效 Final,这是一个好方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27410671/