使用 ActionListeners 确定 Java 作用域

发布于 2024-11-09 09:13:41 字数 3656 浏览 0 评论 0原文

我有一个应用程序需要在用户设置的持续时间内每 10 分钟扫描一次蓝牙设备。

我有两个 javax.swing.Timer (不是 java.util.Timer) - 一个控制每 10 分钟调用一次 Scanning 方法,另一个控制在达到持续时间限制后停止第一个计时器(从而停止扫描)。

urationTimer 在actionListener 中创建并启动。

我的问题是,因为durationTimer是在ActionListener中创建的,所以我无法从另一个ActionListener停止计时器,因为程序无法“看到”变量名称“durationTimer”。

减少的代码如下所示......

public class mainGui extends JFrame

{



public mainGui()

  {

    final ActionListener timerActionEvent = new ActionListener(){

        public void actionPerformed(ActionEvent evt){

            //start a task here

            Timer myTimer2 = (Timer) evt.getSource();

            //Invoke BluetoothScan method

            BluetoothScan(myTimer2);

        }

    };







    final Timer timerDuration;

    final Timer myTimer = new Timer(5000, timerActionEvent); 



    final ActionListener timerDurationActionEvent = new ActionListener(){

        public void actionPerformed(ActionEvent evt){

            //Stops myTimer, so that Bluetooth Stops scanning every 10mins. 

            myTimer.stop();

        }

    };






    ActionListener btnScanAction = new ActionListener(){

    //Action listener for reading data from db

    public void actionPerformed(ActionEvent e){

        int roomID = 0;

        int lecturer = 0;

        int unit;

        int roomIDIndex;

        int lectIDIndex;

        int yearIDIndex;

        int unitIDIndex;

        String[] roomArray;

        String[] lecturerArray;

        String[] unitArray = null;

        int durationIndex;

        String DURATION;

        int durationInt;





        //System.out.println(unitArray.length);

        durationIndex = durCB.getSelectedIndex();

        DURATION = itemDuration[durationIndex];

        durationInt = Integer.parseInt(DURATION);



        //User Selected Duration converted to Milliseconds

        int durationMilliSec = (int)(durationInt*60000);



        ArrayList<String[]> unitYear = null;



        //Store the index ID of the JComboBox Selections

        roomIDIndex = roomCB.getSelectedIndex();

        lectIDIndex = lectCB.getSelectedIndex();

        unitIDIndex = unitCB.getSelectedIndex();

        yearIDIndex = yearCB.getSelectedIndex();



        switch(yearIDIndex)

        {

        case 1: unitYear = Units1; break;

        case 2: unitYear = Units2; break;

        case 3: unitYear = Units3; break;

        case 4: unitYear = UnitsMasters; break;

        }



        //Get the Array contents at index location

        roomArray = rooms.get(roomIDIndex);

        lecturerArray = Lecturers.get(lectIDIndex);

        unitArray = unitYear.get(unitIDIndex);





        if(unitArray==null){

            System.out.println("Please select a unit");

            System.exit(0);

        }



        roomID = Integer.parseInt(roomArray[0]);

        lecturer = Integer.parseInt(lecturerArray[0]);

        unit = Integer.parseInt(unitArray[0]);



        populateComboBoxes pcb = new populateComboBoxes();

        pcb.LabSessionInfo(roomID, lecturer, unit);



        myTimer.start();

        Timer timerDuration = new Timer(durationMilliSec, timerDurationActionEvent);

        timerDuration.start();





        }

    };



public void BluetoothScan(Timer myTimer) {

BluetoothDeviceDiscovery scan = new BluetoothDeviceDiscovery();

try {

    myTimer.stop();

    scan.main();

} catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

} catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

myTimer.start();



};



  }

提前感谢您的任何帮助

I have an application that needs to scan for bluetooth devices every 10 minutes for a duration set by the user.

I have two javax.swing.Timer (not java.util.Timer) - one controls invoking the Scanning method every 10 minutes, the other is stopping the first timer (therfore stopping the scanning) once the duration limit has been hit.

The durationTimer is created and started within an actionListener.

My problem is that because durationTimer is created within an ActionListener, i cannot stop the timer from another ActionListener, as the program cannot "see" the variable name "durationTimer".

The reduced code is shown below....

public class mainGui extends JFrame

{



public mainGui()

  {

    final ActionListener timerActionEvent = new ActionListener(){

        public void actionPerformed(ActionEvent evt){

            //start a task here

            Timer myTimer2 = (Timer) evt.getSource();

            //Invoke BluetoothScan method

            BluetoothScan(myTimer2);

        }

    };







    final Timer timerDuration;

    final Timer myTimer = new Timer(5000, timerActionEvent); 



    final ActionListener timerDurationActionEvent = new ActionListener(){

        public void actionPerformed(ActionEvent evt){

            //Stops myTimer, so that Bluetooth Stops scanning every 10mins. 

            myTimer.stop();

        }

    };






    ActionListener btnScanAction = new ActionListener(){

    //Action listener for reading data from db

    public void actionPerformed(ActionEvent e){

        int roomID = 0;

        int lecturer = 0;

        int unit;

        int roomIDIndex;

        int lectIDIndex;

        int yearIDIndex;

        int unitIDIndex;

        String[] roomArray;

        String[] lecturerArray;

        String[] unitArray = null;

        int durationIndex;

        String DURATION;

        int durationInt;





        //System.out.println(unitArray.length);

        durationIndex = durCB.getSelectedIndex();

        DURATION = itemDuration[durationIndex];

        durationInt = Integer.parseInt(DURATION);



        //User Selected Duration converted to Milliseconds

        int durationMilliSec = (int)(durationInt*60000);



        ArrayList<String[]> unitYear = null;



        //Store the index ID of the JComboBox Selections

        roomIDIndex = roomCB.getSelectedIndex();

        lectIDIndex = lectCB.getSelectedIndex();

        unitIDIndex = unitCB.getSelectedIndex();

        yearIDIndex = yearCB.getSelectedIndex();



        switch(yearIDIndex)

        {

        case 1: unitYear = Units1; break;

        case 2: unitYear = Units2; break;

        case 3: unitYear = Units3; break;

        case 4: unitYear = UnitsMasters; break;

        }



        //Get the Array contents at index location

        roomArray = rooms.get(roomIDIndex);

        lecturerArray = Lecturers.get(lectIDIndex);

        unitArray = unitYear.get(unitIDIndex);





        if(unitArray==null){

            System.out.println("Please select a unit");

            System.exit(0);

        }



        roomID = Integer.parseInt(roomArray[0]);

        lecturer = Integer.parseInt(lecturerArray[0]);

        unit = Integer.parseInt(unitArray[0]);



        populateComboBoxes pcb = new populateComboBoxes();

        pcb.LabSessionInfo(roomID, lecturer, unit);



        myTimer.start();

        Timer timerDuration = new Timer(durationMilliSec, timerDurationActionEvent);

        timerDuration.start();





        }

    };



public void BluetoothScan(Timer myTimer) {

BluetoothDeviceDiscovery scan = new BluetoothDeviceDiscovery();

try {

    myTimer.stop();

    scan.main();

} catch (IOException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

} catch (InterruptedException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

}

myTimer.start();



};



  }

Thankyou in advance for any help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮光沉寂 2024-11-16 09:13:41

您可以在类级别声明durationTimer,并仍然在ActionListener 中构造它。这样它应该在整个班级中可见。在尝试对其调用 stop() 之前,请务必检查它是否不为 null。

另一种选择是让它自行停止:

final ActionListener timerDurationActionEvent = new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        //Stops myTimer, so that Bluetooth Stops scanning every 10mins. 
        myTimer.stop();
        ((Timer)evt.getSource()).stop();
    }
};

另一种(也许是最好的)选择是简单地使其不重复:

    Timer timerDuration = new Timer(durationMilliSec, timerDurationActionEvent);
    timerDuration.setRepeats(false);
    timerDuration.start();

最后一个是要走的路。

You can declare the durationTimer at the class level and still construct it in the ActionListener. This way it should be visible throughout the class. Just be sure to check that it's not null before trying to call stop() on it.

Another option is to have it stop itself:

final ActionListener timerDurationActionEvent = new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        //Stops myTimer, so that Bluetooth Stops scanning every 10mins. 
        myTimer.stop();
        ((Timer)evt.getSource()).stop();
    }
};

And another (and perhaps the best) option is to simply make it non-repeating:

    Timer timerDuration = new Timer(durationMilliSec, timerDurationActionEvent);
    timerDuration.setRepeats(false);
    timerDuration.start();

This last one is the way to go.

稳稳的幸福 2024-11-16 09:13:41

由于您在事件调度线程上启动蓝牙扫描,因此在蓝牙扫描完成之前,第二个计时器不会触发。

您需要为蓝牙扫描启动一个新线程。

我建议你使用 SwingWorker。

Since you start the blue tooth scan on the Event Dispatch thread, you're second Timer will not fire until the blue tooth scan is complete.

You need to start a new thread for the blue tooth scan.

I suggest you use a SwingWorker.

孤檠 2024-11-16 09:13:41

我不知道 btnScanAction 何时被触发(可能经常发生,因为它是一个 ActionListener),但它会创建一个新的
每次调用时的timerDuration。您可能最终会得到多个持续时间计时器,这(我猜......)不是您想要的。

I don't know when btnScanAction is triggered (probably often since it is an ActionListener), but it creates a new
timerDuration at each call. You'll probably end up with multiple duration timers, which (I guess...) is not what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文