我想我已经把自己写进了一个角落。我正在尝试使用 java swing 来达到这种效果。
单击下一步按钮,从文件加载新行(通过行索引号),然后如果文件中该行的日期尚未到达,则将下一步按钮灰显。我的问题是,当我有以下代码时:
Scanner input = new Scanner(System.in);
System.out.println("Enter week number");
int j = input.nextInt();
String[] strArray = new String[4];
xmlLoader(j, strArray);
JButton nextButton = new JButton("Next");
nextButton.setBounds(750, 250, 80, 30);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
j++;
doNext(j, nextButton);
}
});
我不能传递 j 因为它不是最终的,如果它是最终的我不能改变按钮上的任何东西,helpppp!
特定错误:从内部类内部访问局部变量 j;需要声明为final
最佳答案
您可以将j
定义为外部类中的一个字段。
class Sample{
private int j;
void method() {
...
nextButton.setBounds(750, 250, 80, 30);
nextButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
j++;
doNext(j, nextButton);
}
});
}
}
关于Java swing-在点击时执行一个 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9014704/