简体   繁体   English

在一个类中使用ActionListener在另一个类中启动计时器

[英]Using an ActionListener in one class to start a timer in another class

I have a class (simulation) which creates an instance of another class (GUI). 我有一个类(模拟),它创建另一个类(GUI)的实例。 Inside the class GUI there is a button (start) which has an actionlistener attached to it. 在类GUI的内部,有一个按钮(开始),该按钮上附加了一个动作侦听器。

I need this actionlistener to start a timer in simulation but I can't figure out how to do it. 我需要这个动作侦听器来启动仿真中的计时器,但是我不知道该怎么做。

Code in Class Simulation: 类仿真中的代码:

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!");   
}

Code in Class GUI: 类GUI中的代码:

        panel1.add(button1a);

            button1a.addActionListener(new ActionListener() {
                public void actionPerformed (ActionEvent event) {
                    Simulation.StartTimer();
                    }
                } );

The error Eclipse tells me there is, is that for "Simulation.timer.start();" Eclipse告诉我的错误是“ Simulation.timer.start();” :

Cannot make a static reference to the non-static method StartTimer() from the type Simulation. 无法从Simulation类型静态引用非静态方法StartTimer()。

However the method StartTimer() cannot be static as this seems to break the timer... 但是方法StartTimer()不能是静态的,因为这似乎破坏了计时器。

Any help would be very appreciated. 任何帮助将不胜感激。

Pass this as an argument to the GUI constructor. this作为参数传递给GUI构造函数。

In general it is best to avoid such cyclic references. 通常,最好避免使用此类循环引用。 Both the GUI and Simulator become dependent upon one another. GUISimulator相互依赖。 The essence of the solution is to separate out the GUI from the interesting domain-specific behaviour. 解决方案的本质是将GUI与有趣的特定于域的行为分开。

(BTW: I would strongly avoid using static variables for anything other than constants. Also avoid non-private instance variables. But point for not extending JFrame !) (顺便说一句:我会强烈避免对常量以外的任何东西使用静态变量。还要避免使用非私有实例变量。但是不要扩展JFrame !)

There is some hideous boilerplate that you should add to prevent multithreading. 您应该添加一些丑陋的样板,以防止多线程。

public static void main(final String[] args) {
    java.awt.EventQueue.invokeLater(new Runnable() { public void run() {
                Simulation sim = new Simulation();
    }});
}

我要做的是让您的GUI类通过getButton()方法公开按钮,然后在创建GUI对象之后,您的Simulation类可以将其自己的ActionListener添加到按钮,例如control.getButton()。addActionListener(new ActionListener( )...等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM