Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My calculator needs help.

 
LiL Kunji
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy. My question is: is it ok to continue to create the my calc program in the static method main or should i have a calc class with constructs extending JFrame and having static main method instantiate calc and having the windowlistener in it? can i do it both ways? and what are the benefits of both? thanks. here is the code:
import javax.swing.*;
import java.awt.*;
import java .awt.event.*;
public class Calc {
public static void main(String args[]) {

JButton jb[];
String jbNames[] = {"1", "2", "3", "=", "4", "5", "6", "+", "7", "8", "9", "-", "0",

".", "C", "*", "MR", "MC", "M+", "/"};

JFrame jf = new JFrame("Caluculator");
JTextField tf = new JTextField();
JPanel jp = new JPanel (true);
jp.setLayout(new GridLayout(5, 4, 2, 2));

jb = new JButton[jbNames.length];

for (int i = 0; i < jbNames.length; i++) {

jb[i] = new JButton(jbNames[i]);
jp.add(jb[i]);

}
jf.getContentPane().add(tf, BorderLayout.NORTH);
jf.getContentPane().add(jp, BorderLayout.CENTER);
jf.pack();


jf.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent evt) {

System.exit(0);

}

});

jf.setVisible(true);


}
}
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lil,
I would place all the UI code into the constructor instead. The pros are that you can have other methods use the UI components whereas in your case they are only defined for the main method.
You also can't have more than one calculator at one time since you are using a static method to implement it.
You also can't convert your application easily into an Applet because you have used the static main method which is not even used in Applets.
Try to think about program maintenance instead of just slapping some code in a static method. Having a constructor will allow you to use the main method as it was intended to do and test your class out.
Regards,
Manfred.
 
LiL Kunji
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. Is this better?
import javax.swing.*;
import java.awt.*;
import java .awt.event.*;
public class Calc extends JFrame implements ActionListener {

private JFrame jf;
private JTextField tf;
private JPanel jp;
private JButton jb[];
private String jbNames[] = {"1", "2", "3", "=", "4", "5", "6", "+", "7", "8", "9", "-", "0",

".", "C", "*", "MR", "MC", "M+", "/"};
public Calc() {
super("My Little Calculator");
tf = new JTextField();
jp = new JPanel (true);
jp.setLayout(new GridLayout(5, 4, 2, 2));

jb = new JButton[jbNames.length];

for (int i = 0; i < jbNames.length; i++) {

jb[i] = new JButton(jbNames[i]);
jb[i].addActionListener(this);
jp.add(jb[i]);
}
this.getContentPane().add(tf, BorderLayout.NORTH);
this.getContentPane().add(jp, BorderLayout.CENTER);

}

public void actionPerformed(ActionEvent e) {

}

public static void main(String args[]) {

Calc c = new Calc();

c.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent evt) {

System.exit(0);

}
});

c.setVisible(true);
c.pack();

}
}
 
Vinod Venugopal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its better now , but you could even write the windowListener & setVisible code in the constructor, so that u need to only instantiate object of Calc class on the main method.
So your Calc c = new Calc(); statement will now create components ,lay them on the frame, create listeners & display the whole frame too.
Vinod
 
LiL Kunji
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!!! that is great and i will do that. thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic