我有一个JavaFX
8程序(用于跨平台的JavaFXPorts),可以完成我想做的事情,但只差了一步。该程序读取一个文本文件,对行进行计数以建立一个随机范围,从该范围中选择一个随机数,然后读取该行以进行显示。
The error is: local variables referenced from a lambda expression must be final or effectively final
button.setOnAction(e -> l.setText(readln2));
我对Java有点陌生,但是似乎我是否使用Lambda都不显示下一个随机行Label l,我的button.setOnAction(e ->
l.setText(readln2));行期望一个静态值。
有什么想法可以调整我必须在每次按下屏幕按钮时简单显示var readln2的下一个值的内容吗?
在此先感谢,这是我的代码:
String readln2 = null;
in = new BufferedReader(new FileReader("/temp/mantra.txt"));
long linecnt = in.lines().count();
int linenum = rand1.nextInt((int) (linecnt - Low)) + Low;
try {
//open a bufferedReader to file
in = new BufferedReader(new FileReader("/temp/mantra.txt"));
while (linenum > 0) {
//read the next line until the specific line is found
readln2 = in.readLine();
linenum--;
}
in.close();
} catch (IOException e) {
System.out.println("There was a problem:" + e);
}
Button button = new Button("Click the Button");
button.setOnAction(e -> l.setText(readln2));
// error: local variables referenced from a lambda expression must be final or effectively final