在计时器函数中获取<identifier>预期的类型非法启动错误

时间:2019-01-15 19:04:39

标签: java timer timertask

这是我第一次发布,因此如果我做错了,请原谅我。这已经很好地工作了,但是当我将代码放入音频中时,它收到了错误,预期是类型错误的非法开始。我只是从另一个可以正常工作的类中移走了确切的语法。

有什么想法吗?

很抱歉输入了太多代码,但是我希望您看到上下文。现在它在计时器内部,而不在另一个类(文件)中。

您要查找的行在代码中大约向下55行,并在其上方的注释中显示“获取预期的类型非法起始错误”。

谢谢!

代码从这里开始

package boom;

import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;


public class Countdown extends javax.swing.JFrame {

/**
 * Creates new form Countdown
 */   

public Countdown() {
    initComponents();
    Toolkit tk = Toolkit.getDefaultToolkit();
    int xsize = (int) tk.getScreenSize().getWidth();
    int ysize = (int) tk.getScreenSize().getHeight();
    this.setSize(xsize, ysize);



    Timer timer = new Timer(); //new timer


    TimerTask task = new TimerTask() {  
        //counter countIn = new counter();

        //GET COUNTER ENTERED FROM MainMenu and saved in...
        int count1 = counter.counter2.getCounter();

        //Play Countdown
        //TODO: GETTING <identifier> expected illegal start of type error
        PlayAudio.playAudio("AudioFiles/15before.wav");


        String readableText;
        String secondsDisplay;
        String minutesDisplay;

        public void run() { 

            //COUNTDOWN LOGIC
            int minutes = count1/60;
            int seconds = count1%60;

            //convert countdown to readable
            minutesDisplay = Integer.toString(minutes);

            if (seconds >9){
                secondsDisplay = Integer.toString(seconds);
            } else {
                secondsDisplay = "0"+ Integer.toString(seconds);
            }

            readableText = minutesDisplay + ":" + secondsDisplay;

            //send countdown text to label
            countdownLabel.setText(readableText);




            count1 --;
            if (count1 == -1){
                timer.cancel();                                       
            } //else if(isIt){
               // timer.cancel();
               // isIt = false;
           // }

            }

    };

    timer.scheduleAtFixedRate(task, 1000, 1000); // =  timer.scheduleAtFixedRate(task, delay, period);



}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    countdownLabel = new javax.swing.JLabel();
    goToKeypadButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setBackground(new java.awt.Color(0, 0, 0));

    countdownLabel.setBackground(new java.awt.Color(0, 0, 0));
    countdownLabel.setFont(new java.awt.Font("Monospaced", 0, 96)); // NOI18N
    countdownLabel.setForeground(new java.awt.Color(255, 0, 51));
    countdownLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

    goToKeypadButton.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
    goToKeypadButton.setText("Show Keypad");
    goToKeypadButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            goToKeypadButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(goToKeypadButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(countdownLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(15, 15, 15)
            .addComponent(countdownLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(goToKeypadButton, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
    );

    countdownLabel.getAccessibleContext().setAccessibleName("");

    pack();
}// </editor-fold>                        

private void goToKeypadButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                 
    this.setVisible(false);
    new Keypad().setVisible(true);
}                                                

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Countdown.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Countdown.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Countdown.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Countdown.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Countdown().setVisible(true);


        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel countdownLabel;
private javax.swing.JButton goToKeypadButton;
// End of variables declaration                   
}

0 个答案:

没有答案