gpt4 book ai didi

带有 ActionListeners 的 Java 作用域

转载 作者:行者123 更新时间:2023-11-29 08:13:47 26 4
gpt4 key购买 nike

我有一个应用程序需要在用户设置的持续时间内每 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com