The moose likes Swing / AWT / SWT and the fly likes My calculator needs help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "My calculator needs help." Watch "My calculator needs help." New topic
Author

My calculator needs help.

LiL Kunji
Greenhorn

Joined: Jun 23, 2001
Posts: 26
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

Joined: Jan 09, 2001
Posts: 1492
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

Joined: Jun 23, 2001
Posts: 26
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

Joined: Dec 06, 2000
Posts: 148
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


- Vinod<br />-------<br />SCJP2
LiL Kunji
Greenhorn

Joined: Jun 23, 2001
Posts: 26
Thank you very much!!! that is great and i will do that. thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My calculator needs help.