0

我创建了三个按钮来创建不同的图。例如,如果按下噪声,则会显示噪声图表,但如果在按下此噪声后会引发异常。需要进行哪些更改才能使所有按钮都可以工作而无需重复执行。这是代码。

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");
                    }
                }
    }
}
4

1 回答 1

2

Plot考虑一个可以有条件地显示一个或多个系列的单个渲染器,而不是一个概念类的三个不同实例。在此示例中,该setSeriesVisible()方法控制系列的显示,并且每个系列按钮Action都会相应地调用它。同样,Actionfor each plot 按钮更新其自己的系列。

图片

于 2013-06-23T21:19:39.107 回答