使用ActionListeners进行Java范围界定



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

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

durationTimer是在actionListener中创建和启动的。

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

缩减后的代码如下所示。。。。

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();

};

  }

提前感谢的任何帮助

您可以在类级别声明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();

最后一个是要走的路。

由于您在Event Dispatch线程上启动了蓝牙扫描,因此在蓝牙扫描完成之前,您是第二个Timer。

你需要开始一个新的线程为蓝牙扫描。

我建议你使用SwingWorker。

我不知道btnScanAction是什么时候触发的(可能经常是因为它是ActionListener),但它会创建一个新的CCD_ 3。你可能会使用多个持续时间计时器,这(我想…)不是你想要的。

最新更新