The init method is used to initialise variables in the Calculator application. The init() method initialises the layout and background colour of the Calculation applicatin The method also initialises 8 new panel data types and the palcement of each panel on the calculator interface.
Declaration of the method init()
public void init()// Init method is used to initialise the values.
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout(0,7));
// Setting the layout as borderlayout.
// contentPane.setBackground(Color.yellow);
label1=new JLabel(" CALCULATOR ");
memoryTagLabel=new JLabel(" ");
memoryTagLabel.setEnabled(true);
label1.setFont(new Font("Times Roman",Font.BOLD,20));
memoryTagLabel.setFont(new Font("Times Roman",Font.BOLD,17));
memoryTagLabel.setForeground(Color.black);
label1.setForeground(Color.blue);
textarea1=new JTextArea("Developedn Byn Sunni ",4,11);
textarea1.setFont(new Font("Times Roman",Font.BOLD,12));
textarea1.setEditable(false);
boolean wrap = true;
textarea1.setLineWrap(wrap);
degree_cont = new ButtonGroup();//The container of the checkboxes.
JRadioButton degrees = new JRadioButton("Deg",true);
degrees.setActionCommand("Deg");
//degrees.addItemListener(this);
JRadioButton radians = new JRadioButton("Rad",false);
radians.setActionCommand("Rad");
//radians.addItemListener(this);
degree_cont.add(degrees);
degree_cont.add(radians);
button =new Button_class[43];
for(int i=0;i<20;i++)//Creating the Advanced buttons such as, sin & cos & arcsin.
button[i]=new Button_class (Strbutton[i],Color.black);
// new Color(55,168,200)
for(int i=20;i<43;i++)//Creating the numbers.
button[i]=new Button_class (Strbutton[i],Color.black);
textf=new JTextField(22);
textf.setHorizontalAlignment(JTextField.RIGHT );
textf.setFont(new Font("Times Roman", Font.BOLD,12));
textf.addKeyListener(this);
textf.setText("0");
//Constructs panels in the applet.
pa1=new JPanel();
pa2=new JPanel();
pa3=new JPanel();
pa4=new JPanel();
pa5=new JPanel();
pa6=new JPanel();
pa7=new JPanel();
pa8=new JPanel();
pa5.setBounds(20,1,275,30);
pa5.add(button[0]);
pa5.add(label1);
contentPane.add(pa5);
pa3.setLayout(new GridLayout(6,3,2,3));
pa3.setBounds(20,120,225,156);
for (int i=3;i<19;i++)
pa3.add(button[i]);
contentPane.add(pa3);
pa1.setLayout(new GridLayout(3,3,2,2));
pa1.setBounds(20,276,225,78);
for(int i=20;i<29;i++)
pa1.add(button[i]);
contentPane.add(pa1);
pa2.setBounds(20,32,300,90);
pa2.add(textf);
pa2.add(memoryTagLabel);
pa2.add(pa4);
pa4.setBounds(40,50,260,80);
pa4.add(button[1]);
pa4.add(button[2]);
pa4.add(degrees);
pa4.add(radians);
textf.setBackground(Color.black);
contentPane.add(pa2);
pa7.setBounds(320,1,150,182);
pa7.add(textarea1);
pa7.add(button[41]);
pa7.add(button[42]);
contentPane.add(pa7);
pa6.setLayout(new GridLayout(6,2,2,2));
pa6.setBounds(280,196,200,156);
pa8.setBounds(0,0,400,40);
for(int i=29;i<41;i++)
pa6.add(button[i]);
contentPane.add(pa6);
contentPane.add(pa8);
//Before you can use the Calculator you have to press the power button on the calculator interface to enable buttons on the calculator interface.
for(int i=1;i<43;i++)
button[i].setEnabled(false);
textf.setEditable(false);
//Adding the action listener.
for(int i=0;i<43;i++)
button[i].addActionListener(this);
//degrees.addItemListener(this);
//radians.addItemListener(this);
}//End of init method.
The above listing shows how to initialise the layout of the calculator interface. The listing also shows how to add buttons onthe panel. The init() initialises the button enabled property to false. The init() method also initialises the default property for buttons and textboxes.
Declaration of the method init()
public void init()// Init method is used to initialise the values.
{
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout(0,7));
// Setting the layout as borderlayout.
// contentPane.setBackground(Color.yellow);
label1=new JLabel(" CALCULATOR ");
memoryTagLabel=new JLabel(" ");
memoryTagLabel.setEnabled(true);
label1.setFont(new Font("Times Roman",Font.BOLD,20));
memoryTagLabel.setFont(new Font("Times Roman",Font.BOLD,17));
memoryTagLabel.setForeground(Color.black);
label1.setForeground(Color.blue);
textarea1=new JTextArea("Developedn Byn Sunni ",4,11);
textarea1.setFont(new Font("Times Roman",Font.BOLD,12));
textarea1.setEditable(false);
boolean wrap = true;
textarea1.setLineWrap(wrap);
degree_cont = new ButtonGroup();//The container of the checkboxes.
JRadioButton degrees = new JRadioButton("Deg",true);
degrees.setActionCommand("Deg");
//degrees.addItemListener(this);
JRadioButton radians = new JRadioButton("Rad",false);
radians.setActionCommand("Rad");
//radians.addItemListener(this);
degree_cont.add(degrees);
degree_cont.add(radians);
button =new Button_class[43];
for(int i=0;i<20;i++)//Creating the Advanced buttons such as, sin & cos & arcsin.
button[i]=new Button_class (Strbutton[i],Color.black);
// new Color(55,168,200)
for(int i=20;i<43;i++)//Creating the numbers.
button[i]=new Button_class (Strbutton[i],Color.black);
textf=new JTextField(22);
textf.setHorizontalAlignment(JTextField.RIGHT );
textf.setFont(new Font("Times Roman", Font.BOLD,12));
textf.addKeyListener(this);
textf.setText("0");
//Constructs panels in the applet.
pa1=new JPanel();
pa2=new JPanel();
pa3=new JPanel();
pa4=new JPanel();
pa5=new JPanel();
pa6=new JPanel();
pa7=new JPanel();
pa8=new JPanel();
pa5.setBounds(20,1,275,30);
pa5.add(button[0]);
pa5.add(label1);
contentPane.add(pa5);
pa3.setLayout(new GridLayout(6,3,2,3));
pa3.setBounds(20,120,225,156);
for (int i=3;i<19;i++)
pa3.add(button[i]);
contentPane.add(pa3);
pa1.setLayout(new GridLayout(3,3,2,2));
pa1.setBounds(20,276,225,78);
for(int i=20;i<29;i++)
pa1.add(button[i]);
contentPane.add(pa1);
pa2.setBounds(20,32,300,90);
pa2.add(textf);
pa2.add(memoryTagLabel);
pa2.add(pa4);
pa4.setBounds(40,50,260,80);
pa4.add(button[1]);
pa4.add(button[2]);
pa4.add(degrees);
pa4.add(radians);
textf.setBackground(Color.black);
contentPane.add(pa2);
pa7.setBounds(320,1,150,182);
pa7.add(textarea1);
pa7.add(button[41]);
pa7.add(button[42]);
contentPane.add(pa7);
pa6.setLayout(new GridLayout(6,2,2,2));
pa6.setBounds(280,196,200,156);
pa8.setBounds(0,0,400,40);
for(int i=29;i<41;i++)
pa6.add(button[i]);
contentPane.add(pa6);
contentPane.add(pa8);
//Before you can use the Calculator you have to press the power button on the calculator interface to enable buttons on the calculator interface.
for(int i=1;i<43;i++)
button[i].setEnabled(false);
textf.setEditable(false);
//Adding the action listener.
for(int i=0;i<43;i++)
button[i].addActionListener(this);
//degrees.addItemListener(this);
//radians.addItemListener(this);
}//End of init method.
The above listing shows how to initialise the layout of the calculator interface. The listing also shows how to add buttons onthe panel. The init() initialises the button enabled property to false. The init() method also initialises the default property for buttons and textboxes.