我有一个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));
Run Code Online (Sandbox Code Playgroud)
我对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 …Run Code Online (Sandbox Code Playgroud)