不同情节的三个JButtons

时间:2013-06-23 19:09:42

标签: java swing plot jbutton actionlistener

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

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 个答案:

答案 0 :(得分:2)

考虑可以有条件地显示一个或多个系列的单个渲染器,而不是名义Plot类的三个不同实例。在此example中,setSeriesVisible()方法控制系列的显示,每个系列按钮的Action都会相应地调用它。同样,每个绘图按钮的Action也会更新自己的系列。

image