다른 클래스 (GUI)의 인스턴스를 만드는 클래스 (시뮬레이션)가 있습니다. 클래스 GUI에는 actionListener가 첨부 된 버튼 (시작)이 있습니다.한 클래스의 ActionListener를 사용하여 다른 클래스의 타이머 시작
시뮬레이션에서 타이머를 시작하려면이 액션 리스너가 필요하지만 어떻게해야하는지 알 수 없습니다. 클래스 시뮬레이션에서
코드 : 클래스 GUI에서
public class Simulation{
private static JFrame frame;
private static GUI control;
public static Integer xcontrol = 100, ycontrol = 100;
public Timer timer;
public int steps;
public static void main(String[] args) {
Simulation sim = new Simulation();
}
public Simulation() {
frame = new JFrame("Action Listener Test");
frame.setLayout(new BorderLayout(1,0));
control = new GUI (xcontrol, ycontrol);
frame.getContentPane().add(control , BorderLayout.CENTER);
frame.setResizable(false);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void StartTimer() {
timer.start();
System.out.println("It worked!");
}
코드 : "Simulation.timer.start();"
panel1.add(button1a);
button1a.addActionListener(new ActionListener() {
public void actionPerformed (ActionEvent event) {
Simulation.StartTimer();
}
});
이클립스가 나에게 말한다 오류는, 그것을위한입니다 :
시뮬레이션 유형에서 비 정적 메서드 StartTimer()에 대한 정적 참조를 만들 수 없습니다.
이 타이머를 깰 것 같은 방법의 StartTimer는()는 정적이 될 수 없습니다 그러나...
는 어떤 도움을 매우 극명하게 될 것이다.
는 – iny
의의 actionPerformed 이제 읽 Simulation.StartTimer를(); – Myles