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