Hi, this is the blog for programs in different programming languages designd by me. the all programs are original and not copied from any where.
Sunday, October 31, 2010
Java Programming - Calculator in swing
.......................................................
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuiCalculator
{
String o="+";
double d1=0.0,d2=0.0;
JFrame jf;
Container c;
JTextField tf;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
public GuiCalculator()
{
jf=new JFrame("Calculator");
jf.setSize(400,500);
jf.setResizable(false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tf=new JTextField("",12);
c=jf.getContentPane();
//GridBagLayout gb=new GridBagLayout();
//gb.addLayoutComponent(tf);
c.setLayout(null);
tf.setColumns(12);
tf.setHorizontalAlignment(JTextField.RIGHT);
tf.setFont(new Font("Font.TIMESNEWROMAN",Font.BOLD,36));
c.add(tf);
b1=new JButton("1");
b2=new JButton("2");
b3=new JButton("3");
b4=new JButton("4");
b5=new JButton("5");
b6=new JButton("6");
b7=new JButton("7");
b8=new JButton("8");
b9=new JButton("9");
b10=new JButton("0");
b11=new JButton(".");
b12=new JButton("=");
b13=new JButton("+");
b14=new JButton("-");
b15=new JButton("x");
b16=new JButton("/");
b17=new JButton("");
tf.setBounds(20,10,330,50);
b1.setBounds(10,120,60,35);
b2.setBounds(110,120,60,35);
b3.setBounds(210,120,60,35);
b4.setBounds(310,120,60,35);
b5.setBounds(10,220,60,35);
b6.setBounds(110,220,60,35);
b7.setBounds(210,220,60,35);
b8.setBounds(310,220,60,35);
b9.setBounds(10,320,60,35);
b10.setBounds(110,320,60,35);
b11.setBounds(210,320,60,35);
b12.setBounds(310,320,60,35);
b13.setBounds(10,420,60,35);
b14.setBounds(110,420,60,35);
b15.setBounds(210,420,60,35);
b16.setBounds(310,420,60,35);
b17.setBounds(310,420,60,35);
//c.setLayout(new GridLayout(4,4));
b1.addActionListener(new Perform());
b2.addActionListener(new Perform());
b3.addActionListener(new Perform());
b4.addActionListener(new Perform());
b5.addActionListener(new Perform());
b6.addActionListener(new Perform());
b7.addActionListener(new Perform());
b8.addActionListener(new Perform());
b9.addActionListener(new Perform());
b10.addActionListener(new Perform());
b11.addActionListener(new Perform());
b12.addActionListener(new Perform());
b13.addActionListener(new Perform());
b14.addActionListener(new Perform());
b15.addActionListener(new Perform());
b16.addActionListener(new Perform());
c.add(b1);
c.add(b2);
c.add(b3);
c.add(b4);
c.add(b5);
c.add(b6);
c.add(b7);
c.add(b8);
c.add(b9);
c.add(b10);
c.add(b11);
c.add(b12);
c.add(b13);
c.add(b14);
c.add(b15);
c.add(b16);
c.add(b17);
jf.setVisible(true);
}
class Perform implements ActionListener
{
//String o="+";
//double d1=0.0,d2=0.0;
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
int j=-1;
if(Character.isDigit(s.charAt(0)))
j=Integer.parseInt(s);
for(int i=0;i<=9;i++)
if(i==j)
tf.setText(tf.getText()+s);
if((s.equals("+"))||(s.equals("-"))||(s.equals("x"))||(s.equals("/")))
{
if(!(tf.getText().equals("")))
d1=Double.parseDouble(tf.getText());
o=s;
tf.setText("");
}
if(s.equals("="))
{
d2=Double.parseDouble(tf.getText());
if(o.equals("+"))
tf.setText(String.valueOf(d1+d2));
if(o.equals("-"))
tf.setText(String.valueOf(d1-d2));
if(o.equals("x"))
tf.setText(String.valueOf(d1*d2));
if(o.equals("/"))
tf.setText(String.valueOf(d1/d2));
}
}
}
public static void main(String args[])
{
GuiCalculator gui=new GuiCalculator();
}
}
Thursday, October 7, 2010
Subscribe to:
Posts (Atom)