제어 배경을 변경할 수있는 정적 클래스를 작성하려고합니다. 매개 변수에 전달됩니다. 그래서 나는이 달성 :비 최종 변수 및 타이머를 참조 할 수 없습니다.
public static void wrong(final Component component) {
component.setBackground(Color.RED);
Timer timer = new Timer(2, wrongAction);
wrongAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int green = component.getBackground().getGreen();
int blue = component.getBackground().getBlue();
component.setBackground(new Color(255, green + 1, blue + 1));
if (component.getBackground() == Color.WHITE) {
timer.stop();
}
}
};
timer.start();
}
을 그리고 오류를 가지고 : 물론
Cannot refer to a non-final variable timer inside an inner class defined in a different method
, 우리는 마지막에 타이머를 변경할 수 있지만, 우리가 그렇게 후에 방법은 작동을 멈 춥니 다.
나는 그것을 시도하고 다른 stackoverflow 주제에 대한 답변을 찾으려고했지만 아무것도 도움이되지 않았다.
미리 감사드립니다.
를 추가 한 다음 생성자에서 널을 통과 할 수 있습니다 (http://sscce.org/). BTW - 질문 있니? 이게 뭐야? –
타이머를 마지막으로 만든 후에는 메소드가 작동을 멈추게합니까? –
"최종 타이머 타이머 ="로 변경하십시오. 그것은 나를 위해 잘 작동합니다. – Adam