带有 ActionListeners 的 Java 作用域

标签 java swing timer

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

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

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



};



  }

提前感谢您的帮助

最佳答案

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

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

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

最后一个是要走的路。

关于带有 ActionListeners 的 Java 作用域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6099622/

相关文章:

Contiki 时间戳值为 int

c++ - 在 Linux 中使用 C++ 实现低开销间隔定时器

带有通配符的 Java 泛型无法编译

java - 绘制形状在graphics2D中不起作用

java - 从表示数据库行的 Map<String,String> 创建 JPA 实体

java - 线程 "AWT-EventQueue-0"java.lang.NullPointerException 中的异常? java

java - 当我在 JTable 中搜索时,如何以编程方式从 JTable 中选择特定文本?

java - 如何将 Swing 计时器与 JPanel 一起使用

java - 如何读取 +PrintTenuringDistribution 的输出

Java 预处理器检查操作系统