2011-05-31 148 views
0

我是一個Java新手,所以請耐心等待我的幫助。非靜態變量不能從靜態上下文中引用

我打算先錄音然後錄音。

我使用netbeans IDE 6.8。這裏是代碼:

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.io.*; 
import javax.sound.sampled.*; 
import java.awt.event.KeyListener; 
import java.io.File; 
import java.io.IOException; 


public class Reg1 extends javax.swing.JFrame { 
    AudioFormat audioFormat; 
    TargetDataLine targetDataLine; 
     public Reg1() { 
     initComponents(); 
    } 

    /** 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() { 

     jButton1 = new javax.swing.JButton(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 

     jButton1.setText("Stop"); 
     jButton1.addMouseListener(new java.awt.event.MouseAdapter() { 
      public void mouseReleased(java.awt.event.MouseEvent evt) { 
       jButton1MouseReleased(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() 
       .addContainerGap(211, Short.MAX_VALUE) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(237, 237, 237)) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
       .addContainerGap(287, Short.MAX_VALUE) 
       .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(65, 65, 65)) 
     ); 

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

    private void jButton1MouseReleased(java.awt.event.MouseEvent evt) { 

     targetDataLine.stop(); 
     targetDataLine.close(); 

    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String args[]) { 
     java.awt.EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       new Reg1(); 
      } 
      private void Reg1(){ 

       KeyListener s; 

       try { 
      AudioInputStream audio = AudioSystem.getAudioInputStream(new File("name.wav")); 
      Clip clip = AudioSystem.getClip(); 
      clip.open(audio); 
      clip.start(); 
     } 

     catch(UnsupportedAudioFileException uae) { 
      System.out.println(uae); 
     } 
     catch(IOException ioe) { 
      System.out.println(ioe); 
     } 
     catch(LineUnavailableException lua) { 
      System.out.println(lua); 
     } 
       captureAudio(); 

      } 
     }); 
    } 
    private void captureAudio(){ 
    try{ 

     audioFormat = getAudioFormat(); 
     DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class,audioFormat); 
     targetDataLine = (TargetDataLine) 
       AudioSystem.getLine(dataLineInfo); 


     new CaptureThread().start(); 
    }catch (Exception e) { 
     e.printStackTrace(); 
     System.exit(0); 
    } 
    } 


    public static AudioFormat getAudioFormat(){ 

    float sampleRate = 8000.0F;  
    int sampleSizeInBits = 16;  
    int channels = 1;  
    boolean signed = true;  
    boolean bigEndian = false; 
    return new AudioFormat(sampleRate, 
          sampleSizeInBits, 
          channels, 
          signed, 
          bigEndian); 
    } 

class CaptureThread extends Thread{ 

    public void run(){ 
    AudioFileFormat.Type fileType = null; 
    File audioFile = null; 
    fileType = AudioFileFormat.Type.WAVE; 
    audioFile = new File("name.wav"); 
    try{ 
     targetDataLine.open(audioFormat); 
     targetDataLine.start(); 
     AudioSystem.write(
      new AudioInputStream(targetDataLine), 
      fileType, 
      audioFile); 
    }catch (Exception e){ 
     e.printStackTrace(); 
    } 

    } 
} 


    // Variables declaration - do not modify 
    private javax.swing.JButton jButton1; 
    // End of variables declaration 

} 

請給出具體的方向,因爲我是Java編程的noob。

+1

編譯器在哪一行抱怨? – 2011-05-31 09:18:04

+1

這是很多代碼 - 你能更具體地說明哪一行導致錯誤?這通常由相關鏈接來判斷,你是否閱讀過這些內容? – 2011-05-31 09:19:03

+1

這是相當明顯的調用'captureAudio()'的'的main()'方法,以及其他潛在的問題 – 2011-05-31 09:23:23

回答

2

靜態意味着沒有對象(類的實例,如new YourClass())。如果你有一個靜態函數,它應該是「獨立的」,所以它不能使用非靜態的東西,因爲它們使用真實對象中的東西(如成員變量等)

任何static函數只能調用並使用其他static函數。所以在那個環境中尋找麻煩。

+0

thanksthanksthanks亞勒...它解決 – 2011-06-02 08:40:24

3

雖然可能有更正式的資源來解釋這種(如Java語言規範),我經常發現自己檢查這個網頁,這些題型:

http://mindprod.com/jgloss/jgloss.html

它解釋這些事情非常好,也爲菜鳥;-) 你的情況:

+0

thanksthanks亞勒...它解決 – 2011-06-02 08:40:19

+0

@Karan,請「接受」最適合的答案,點擊「接受」按鈕 – 2011-06-02 09:57:56

2

更換你的主要有:

public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      Reg1 r = new Reg1(); 
      try { 
       AudioInputStream audio = AudioSystem.getAudioInputStream(new File("name.wav")); 
       Clip clip = AudioSystem.getClip(); 
       clip.open(audio); 
       clip.start(); 
      } 
      catch (Exception e) { 
       System.out.println(e); 
      } 
      r.captureAudio(); 
     } 
    }); 
} 
+0

感謝亞勒......也就迎刃而解了 – 2011-06-02 08:40:06

3

我用了 「Java編輯器」,我試圖編譯你的類。我得到了錯誤masseage

Reg1.java:93:16:非靜態方法captureAudio()不能從靜態上下文

原因引用的是:您在方法調用 void main()一個方法,它需要一個對象的實例。如果一個方法不爲靜態宣佈你需要follwoing語法「referenceObject.captureAudio()」

0

captureAudio()它是一個實例方法,它不能在main()沒有對象稱爲是靜態的不聲明爲static從而。

正弦我看你已經在Reg1的run methid新實例中創建了,因此對於此對象調用方法captureAudio()是好的。另外,不需要內置的private void Reg1()方法,它可以讓你永遠不會打電話。我建議你將main改爲這樣的東西。

public static void main(String args[]) 
    { 
     java.awt.EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       Reg1 reg1 = new Reg1(); 
       KeyListener s;  
       try 
       { 
        AudioInputStream audio = AudioSystem.getAudioInputStream(new File("name.wav")); 
        Clip clip = AudioSystem.getClip(); 
        clip.open(audio); 
        clip.start(); 
       }catch(UnsupportedAudioFileException uae) 
       { 
        System.out.println(uae); 
       }catch(IOException ioe) 
       { 
        System.out.println(ioe); 
       }catch(LineUnavailableException lua) 
       { 
        System.out.println(lua); 
       } 
       reg1.captureAudio(); 
      } 
     }); 
    } 
+0

得益於它解決..我會所有的人繼續在未來給你麻煩....... – 2011-06-02 08:38:41

相關問題