2013-06-23 33 views
0

我創建了三個按鈕,創建不同的地塊。例如如果按下了噪聲,則會顯示噪聲圖表,但是如果在按下此噪聲後按下,則會引發異常。需要進行哪些更改才能使所有按鈕都可以在不重複執行的情況下工這是代碼。三個JButtons爲不同的地塊

package project; 

    /* 
    * @author Abdul Baseer 
    */ 
    public class Buttons extends JFrame{ 
     protected JButton Noise, Noisy, Filtered,FFT; 
     public Buttons(){ 
      super("Adaptive Filtering"); 
      setLayout(new FlowLayout()); 

     Noise=new JButton("Noise Data"); 
     Noise.setBackground(Color.red); 
     add(Noise); 
     Noisy=new JButton("Noise+Data"); 
     Noisy.setBackground(Color.yellow); 
     add(Noisy); 
     Filtered=new JButton("Filtered Data"); 
     Filtered.setBackground(Color.green); 
     add(Filtered); 
     ButtonHandler handler=new ButtonHandler(); 
     Noisy.addActionListener(handler); 
     Noise.addActionListener(handler); 
     Filtered.addActionListener(handler); 

    } 
    private class ButtonHandler implements ActionListener{ 
       String t = JOptionPane.showInputDialog("Observation Time in seconds"); 
       double T=Double.parseDouble(t); 
       @Override 
       public void actionPerformed(ActionEvent event){ 
       try { 
       Capture c=new Capture(); 
       byte[]b = c.Capture(T);  
       int n=b.length; 
       double[]X=new double[n/4]; 
       double[]Y=new double[n/4]; 
       for(int i=0;i<n/4;i++){ 
        X[i]=(3*((b[i*4]&0xff)|(b[i*4+1]<<8))/32768.0f); 
        Y[i]=((b[i*4+2]&0xff)|(b[i*4+3]<<8))/32768.0f; 
       } 
       if(event.getSource()==Noise){ 
        Plot px=new Plot("Noise","Samples","Amplitude",X); 
        FFT F=new FFT(); 
        double[]XX=F.FFT(X); 
        Plot fxp=new Plot("Noise Spectrum","Samples","Magnitude|X[k|",XX);        
          } 
       if(event.getSource()==Noisy){ 
        Plot py=new Plot("Noisy","Samples","Amplitude",Y); 
        FFT F=new FFT(); 
        double[]YY=F.FFT(Y); 
        Plot fyp=new Plot("Noisy Spectrum","Samples","Magnitude|Y[k|",YY);  
          } 
       if(event.getSource()==Filtered){  
        String l = JOptionPane.showInputDialog("Filter Order"); 
        int L=Integer.parseInt(l); 
        String u = JOptionPane.showInputDialog("Step Size"); 
        double U=Double.parseDouble(u); 
        LMS LM=new LMS(); 
        double[]E=LM.LMS(L,U,X, Y); 
        ComPlot pe=new ComPlot("",Y,E); 
        FFT F=new FFT(); 
        double[]EE=F.FFT(E); 
        Plot fep=new Plot("Filtered Spectrum","Samples","Magnitude|E[k|",EE);   
          } 
       } catch (LineUnavailableException | HeadlessException | NumberFormatException ex) { 
         System.out.println("Unsupported"); 
        } 
       } 
    } 
} 
+1

什麼行導致異常被拋出?你可以發佈你的異常的stacktrace?它非常有助於我們在所有的細節中真正看到例外。 –

+0

另外,你的catch塊是非常不能提供信息的。考慮在那裏有一個'ex.printStackTrace()'。 –

+0

遵循Java命名約定。變量名稱不應以大寫字符開頭。 – camickr

回答

2

取而代之的名義Plot類的三種不同的情況下,可以考慮一個單一的渲染器,可以有條件地顯示一個或多個系列。在此example中,setSeriesVisible()方法控制一系列的顯示,並且每個系列按鈕的Action都相應地調用它。同樣,每個圖表按鈕的Action都會更新其自己的系列。

image

相關問題