簡體   English   中英

從Java中的ActionListener內調用Timer

[英]calling a Timer from inside an ActionListener in Java

我已經用Java創建了一個(第二個)計時器,但是我沒有在創建計時器時添加爭論(int,動作),而是嘗試在動作偵聽器中初始化計時器。 原因是[在計時器爭論中] 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();
    }
}

問題在創建計時器timerDuration(durationMilliSec,timerDurationActionEvent).start()時位於btnScanAction ActionListener的底部。

這是無法從ActionListener中看到創建的Timer的問題。 有誰知道解決這個問題的可能方法,仍然保留timerDuration(durationMilliSec,timerDurationActionEvent).start(); 在ActionListener中?

非常感謝

timerDuration(durationMilliSec, timerDurationActionEvent).start();

這里沒有任何意義。 Java希望在這里調用方法。 如果要在此處啟動新的Timer(),請編寫

Timer timer = new TimerTask() {
 // code here
}.start();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM