2015-01-13 3 views
-2

시간을 입력 할 수있는 간단한 Java GUI를 만들려고하는데 시간이 다되면 소리가납니다. 어떤 이유로 나는 Timer 나 Handler를 사용하라고 알려주는 많은 다른 소스를보고 있음에도 불구하고 오류를 발견합니다. 나는 내가 schedule에 오류에 얻을 라인 new Timer().schedule(new TimerTask() { 에 도착하면Java 타이머가 작동하지 않는 이유는 무엇입니까?

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
/** 
* 
* @author CIS 
*/ 
import java.io.*; 
import sun.audio.*; 
import java.util.TimerTask; 


public class Timer extends javax.swing.JFrame { 


    public Timer() { 
     initComponents(); 
     initEditable(); 
    } 

    AudioPlayer MGP = AudioPlayer.player; 
     AudioStream BGM; 
     AudioData MD; 

     ContinuousAudioDataStream loop1 = null; 
    AudioPlayer ABC = AudioPlayer.player; 
     AudioStream ABCD; 
     AudioData ABCDE; 

     ContinuousAudioDataStream loop2 = null; 

    public void initEditable(){ 

} 

    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     textFieldTimer = new javax.swing.JTextField(); 
     buttonTest = new javax.swing.JButton(); 
     buttonSet = new javax.swing.JButton(); 
     labelTimeRemaining = new javax.swing.JLabel(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     buttonTest.setText("Test Sound"); 
     buttonTest.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       buttonTestActionPerformed(evt); 
      } 
     }); 

     buttonSet.setText("Set"); 
     buttonSet.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       buttonSetActionPerformed(evt); 
      } 
     }); 

     labelTimeRemaining.setText("time remaing"); 

     javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
     getContentPane().setLayout(layout); 
     layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) 
        .addComponent(textFieldTimer) 
        .addComponent(buttonTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
        .addComponent(buttonSet) 
        .addComponent(labelTimeRemaining))) 
     ); 

     layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {buttonSet, buttonTest, labelTimeRemaining, textFieldTimer}); 

     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(textFieldTimer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addComponent(labelTimeRemaining)) 
       .addGap(18, 18, 18) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) 
        .addComponent(buttonTest) 
        .addComponent(buttonSet))) 
     ); 

     pack(); 
    }// </editor-fold>       
    int hello = 1000; 

    public void music1(){ 
     ContinuousAudioDataStream loop = null; 
     try 
     { 
      InputStream alert = new FileInputStream("alert.wav"); 
      BGM = new AudioStream(alert); 
      AudioPlayer.player.start(BGM); 

      //MD = BGM.getData(); 
      //loop = new ContinuousAudioDataStream(MD) 
     } 
     catch(FileNotFoundException e){ 
      System.out.print(e.toString()); 
     } 
     catch(IOException error) 
     { 
      System.out.print(error.toString()); 

     } 
     MGP.start(loop1); 

     } 

    private void buttonSetActionPerformed(java.awt.event.ActionEvent evt) {           

    new Timer().schedule(new TimerTask() {   
    @Override 
    public void run() { 
     music1();  
    } 
}, 2000); 

    }           

    private void buttonTestActionPerformed(java.awt.event.ActionEvent evt) {           
     music1(); 
    }           


    /** 
    * @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(Timer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (InstantiationException ex) { 
      java.util.logging.Logger.getLogger(Timer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (IllegalAccessException ex) { 
      java.util.logging.Logger.getLogger(Timer.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
     } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
      java.util.logging.Logger.getLogger(Timer.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 Timer().setVisible(true); 
      } 
     }); 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JButton buttonSet; 
    private javax.swing.JButton buttonTest; 
    private javax.swing.JLabel labelTimeRemaining; 
    private javax.swing.JTextField textFieldTimer; 
    // End of variables declaration     





} 

: 여기 내 코드입니다. 내 문제를 제시하는 나의 방법이 올바르지 않다면 사과드립니다. 저는 여기에서 새롭고 Java 프로그래밍에 익숙합니다.

답변

0

클래스의 이름은 java.util.Timer 클래스와 같습니다. 해당 클래스 이름을 정규화하여 명확하게 구분해야합니다.

new java.util.Timer().schedule(new TimerTask() { 
+0

고맙습니다. – Aria

+0

환영합니다 :) 문제가 해결되면 제 대답을 수락하십시오. – gknicker

+0

내 대답을 수락하면 무슨 뜻인지 모르겠습니다. 문제를 해결해 주셔서 감사합니다. 투표를 의미하는 것이라면 그 일을하려고했지만 적어도 그렇게하기 위해서는 평판이 15 개 이상 필요합니다. 죄송합니다 : | – Aria

관련 문제