从 Java 中的 ActionListener 内部调用计时器

发布于 2024-11-08 20:30:12 字数 4765 浏览 0 评论 0原文

我在 Java 中创建了一个(第二个)计时器,但我没有在创建计时器时添加参数(int,action),而是尝试在动作侦听器中初始化计时器。 原因是 int [在计时器争论中] 是在动作侦听器中创建的。

但当我这样做时,找不到计时器。

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.Border;

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();
                //myTimer2.stop();
                BluetoothScan( myTimer2 );
            }
        };
        final ActionListener timerDurationActionEvent = new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
                //Create a method to stop both timers at the bottom of this class
                Timer myTimer3 = (Timer)evt.getSource();
                StopTimers( myTimer3 );
            }
        };
        final Timer timerDuration;
        final Timer myTimer = new Timer( 5000, timerActionEvent );

        /*
         * Start All ActionListeners // ItemListeners
         */

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

                //HERE IS MY PROBLEM

                timerDuration( durationMilliSec, timerDurationActionEvent ).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();
    };
    public void StopTimers( Timer timerDuration ) {
        timerDuration.stop();
        //myTimer.stop();
    }
}

问题出在创建计时器时的 btnScanAction ActionListener 的底部。

这是无法从 ActionListener 中看到创建的计时器的问题。有谁知道解决这个问题的可能方法,仍然保留timerDuration(durationMilliSec,timerDurationActionEvent).start();在动作监听器中??

非常感谢大家

I have created a (2nd) Timer in Java but rather than adding the arguements (int, action) when creating the Timer, I am trying to initialize the Timer inside an actionlistener.
The reason is that the int [in the timers arguement] is created within the actionlistener.

When i do this though, the Timer cant be found.

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.border.Border;

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();
                //myTimer2.stop();
                BluetoothScan( myTimer2 );
            }
        };
        final ActionListener timerDurationActionEvent = new ActionListener() {
            public void actionPerformed( ActionEvent evt ) {
                //Create a method to stop both timers at the bottom of this class
                Timer myTimer3 = (Timer)evt.getSource();
                StopTimers( myTimer3 );
            }
        };
        final Timer timerDuration;
        final Timer myTimer = new Timer( 5000, timerActionEvent );

        /*
         * Start All ActionListeners // ItemListeners
         */

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

                //HERE IS MY PROBLEM

                timerDuration( durationMilliSec, timerDurationActionEvent ).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();
    };
    public void StopTimers( Timer timerDuration ) {
        timerDuration.stop();
        //myTimer.stop();
    }
}

The problem lies at the bottom of the btnScanAction ActionListener when creating the Timer timerDuration(durationMilliSec, timerDurationActionEvent).start();

its a problem with not being able to see the created Timer from within the ActionListener. Does anyone know of a possible way round this, still keeping the timerDuration(durationMilliSec, timerDurationActionEvent).start(); In the ActionListener??

Thanks a lot guys

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

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

发布评论

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

评论(1

游魂 2024-11-15 20:30:12
timerDuration(durationMilliSec, timerDurationActionEvent).start();

这里没有任何意义。 Java 期望在这里调用方法。
如果你想在这里启动新的Timer(),那么写

Timer timer = new TimerTask() {
 // code here
}.start();
timerDuration(durationMilliSec, timerDurationActionEvent).start();

Doesn't make any sence here. Java expects method call here.
If you want to start new Timer() here, then write

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