Hi everybody,It's the first time for me to deal with GUI,actually I know nothing about it.I've done a lot in the code all what I need to know why this code not displaying the frames as excepected and what should be done to make it perform what it should do:
CODE
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Interface extends JFrame
{
int y,x;
int c=3;
double[]r;
int[]t;
boolean b;
JButton button1,button2,button3,button4,button5,button6,button7,button8,button9,button10
;
JRadioButton jrb1,jrb2;
MyButtonListener bl=new MyButtonListener();
Interface in=new Interface();
JFrame f1 = new JFrame ("Statistics Program");
JFrame f2=new JFrame();
JFrame f3=new JFrame();
JFrame f4=new JFrame();
JFrame f5=new JFrame();
JFrame f7=new JFrame();
Container container=getContentPane();
public static void main(String[]args)
{
Interface in=new Interface();
in.screen1();
}
public static void frame(Frame f)
{
//for using a specific skin for the appearence of the window
JFrame.setDefaultLookAndFeelDecorated(true);
f.setSize(700,700);
//make frame in the center of the screen
//dimentions of the screen
Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth=screenSize.width;
int screenHeight=screenSize.height;
int q=(screenWidth-f.getWidth())/2;
int k=(screenHeight-f.getHeight())/2;
//specify the location of the window in the screen and make it visible to user
f.setLocation(q,k);
}
public void screen1()
{
JLabel label1=new JLabel("Welcome ! ");
Font myfont=new Font("Serif",Font.BOLD+Font.ITALIC,30);
label1.setFont(myfont);
JLabel label2=new JLabel("This program helps you to test the goodness of fit for a set of data ");
label1.setHorizontalAlignment(JLabel.CENTER);
label2.setHorizontalAlignment(JLabel.CENTER);
button1=new JButton("Ok,let's start");
button1.addActionListener(bl);
//to insert contents in the container
container.add(label1);
container.add(label2);
frame(f1);
f1.setVisible(true);
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void screen2()
{
frame(f2);
jrb1=new JRadioButton("3");
jrb2=new JRadioButton("4");
button2=new JButton("Next");
container.add(jrb1);
container.add(jrb2);
container.add(button2);
}
// to generate textboxes,labels for the problem data
public void screen3(int x)
{
r=new double[4];
t=new int[r.length];
for(int i=0;i<x;i++)
{
//obtain size of sample
JLabel label=new JLabel("n = ");
JTextField textField=new JTextField();
y=Integer.parseInt(textField.getText());
//obtain array of probabilities
JLabel label1=new JLabel("P"+i+" = ");
JTextField textField1=new JTextField();
r[i]=Double.parseDouble(textField1.getText());
textField1.setLayout(new FlowLayout(FlowLayout.LEFT,10,20));
//obtain array of observations
JLabel label2=new JLabel("O"+i+" = ");
JTextField textField2=new JTextField();
t[i]=Integer.parseInt(textField2.getText());
label2.setLayout(new FlowLayout(FlowLayout.LEFT,20,20));
textField2.setLayout(new FlowLayout(FlowLayout.LEFT,30,20));
container.add(label);
container.add(textField);
container.add(label1);
container.add(textField1);
container.add(label2);
container.add(textField2);
double[] e=expected(r,y,x);
if((testStatic(t, r, x))>(e[i]))
b=false;
else
b=true;
}
frame(f3);
button3=new JButton("Back");
button4=new JButton("Next");
in.test(r,x);
container.add(button3);
container.add(button4);
}
public void screen4()
{
frame(f4);
JLabel label=new JLabel("ERROR!/nThe sum of probabilities mustn't exceed 1");
button5=new JButton("return");
container.add(label);
container.add(button5);
}
public void screen5(double[]r,int c,int[] t)
{
String u;
String s="At alpha=.05 :/nH0: the probabilities are P1="+r[0]+", P2="+
r[1]+", P3="+r[2];
if(c==4)
s+=", P4="+r[3]+"\n";
if(in.b==true)
u="\nChi-Square="+testStatic(t, r, x)+"\nAccept H0";
else
u="\nChi-Square="+testStatic(t, r, x)+"\nReject H0";
frame(f5);
JLabel label=new JLabel( s+u);
button5=new JButton("Back");
button6=new JButton("Ok");
button7=new JButton("Start another one");
container.add(label);
container.add(button5);
container.add(button6);
container.add(button7);
}
public void screen7()
{
frame(f7);
JLabel label=new JLabel("Thanks/nany comments could be sent to:/nmonmon_3200@hotmail.com/n&/nsupper_moha6000@yahoo.com");
button8=new JButton("Back");
button9=new JButton("Goodbye");
}
//test whether sum of probabilities exceeds 1 or not
public void test(double[]r,int numOfCells)
{
double sumOfP=0;
for(int w=0; w<numOfCells; w++)
{
r[w]=(int)(r[w]*100)/100.0;
sumOfP+=r[w];
}
//to ensure that sum of p always = 1
if (sumOfP!=1.0)
{
in.screen4();
}
}
//For determining value of x^2 alfa
public static double alfa(int numOfCells)
{
double chiSquare=0;
if(numOfCells==2)
return 5.991;
else
return 7.815;
}
//For calculating excepected values :
public static double[] expected(double[] probability, int size, int numOfCells)
{
double[] expectedValues=new double[numOfCells];
for(int x=0; x<numOfCells; x++)
{
expectedValues[x]=probability[x]*size;
}
return expectedValues;
}
//For calculating x^2
public static double testStatic(int[] numbers, double[] probability, int numOfCells)
{
double chiSquare=0;
for(int x=0; x<numOfCells; x++)
{
chiSquare +=( (Math.pow( (numbers[x]-probability[x]) , 2)) / probability[x] );
}
return chiSquare;
}
}
class MyButtonListener implements ActionListener
{
Interface in=new Interface();
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==in.jrb1)
{
in.screen3(in.c);
in.f2.setVisible(false);
}
else
in.c=4;
in.screen3(in.c);
in.f2.setVisible(false);
if(e.getSource() ==in.button1)
in.screen2();
in.f1.setVisible(false);
if(e.getSource() ==in.button2|e.getSource() ==in.button5|e.getSource() ==in.button6)
{
in.screen3(in.c);
in.f2.setVisible(false);
in.f5.setVisible(false);
}
else
{
if(e.getSource() ==in.button3|e.getSource() ==in.button8)
{
in.screen2();
in.f3.setVisible(false);
in.f5.setVisible(false);
}
if(e.getSource() ==in.button4)
in.test(in.r,in.c);
if(e.getSource() ==in.button7)
in.screen7();
if(e.getSource() ==in.button9)
in.screen5(in.r,in.c,in.t);
else
System.exit(0);
}
}
}