Search This Blog & Web

Tuesday, January 3, 2012

Calculator GUI & ActionListner (JAVA) :D

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;


class MyFrame extends JFrame implements ActionListener{

JPanel jp;
JPanel p;
JPanel p1;
JPanel p2;
JButton b0;
JButton b1;
JButton b2;
JButton b3;
JButton bt0;
JButton bt1;
JButton bt2;
JButton bt3;
JButton bt4;
JButton bt5;
JButton bt6;
JButton bt7;
JButton bt8;
JButton bt9;
JButton bt10;
JButton bt11;
JLabel lbl1;
    JTextField txtDisplay;
    JButton clear;
    int n1;
int n2;
char operator;
 
MyFrame(){

b0=new JButton("7");
b1=new JButton("8");
b2=new JButton("9");
b3=new JButton("/");
bt0=new JButton("4");
bt1=new JButton("5");
bt2=new JButton("6");
bt3=new JButton("*");
bt4=new JButton("1");
bt5=new JButton("2");
bt6=new JButton("3");
bt7=new JButton("-");
bt8=new JButton("0");
bt9=new JButton(".");
bt10=new JButton("=");
bt11=new JButton("+");
clear=new JButton("Clear");

b0.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
bt0.addActionListener(this);
    bt1.addActionListener(this);
bt2.addActionListener(this);
bt3.addActionListener(this);
bt4.addActionListener(this);
bt5.addActionListener(this);
bt6.addActionListener(this);
bt7.addActionListener(this);
bt8.addActionListener(this);
bt9.addActionListener(this);
bt10.addActionListener(this);
bt11.addActionListener(this);
clear.addActionListener(this);

jp=new JPanel();
txtDisplay=new JTextField(20);
jp.add(txtDisplay);
add(jp,"North");

p=new JPanel();
p.setLayout(new GridLayout(4,3));
p2=new JPanel(new FlowLayout(FlowLayout.LEFT));
lbl1=new JLabel("<html>CopyRight By DT Lakmal<br> All Rights Reserved");

p2.add(lbl1);
p.add(b0);
p.add(b1);
p.add(b2);

p.add(bt0);
p.add(bt1);
p.add(bt2);

p.add(bt4);
p.add(bt5);
p.add(bt6);

p.add(bt8);
p.add(bt9);
p.add(bt10);

add(p,"Center");
add(p2,"South");

p1=new JPanel();
p1.setLayout(new GridLayout(5,1,4,5));
p1.add(b3);
p1.add(bt3);
p1.add(bt7);
p1.add(bt11);
p1.add(clear);
add(p1,"East");
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==bt4){
txtDisplay.setText(txtDisplay.getText()+"1");
}
if(e.getSource()==bt5){
txtDisplay.setText(txtDisplay.getText()+"2");
}
if(e.getSource()==bt6){
txtDisplay.setText(txtDisplay.getText()+"3");
}
if(e.getSource()==bt0){
txtDisplay.setText(txtDisplay.getText()+"4");
}
if(e.getSource()==bt8){
txtDisplay.setText(txtDisplay.getText()+"0");
}
if(e.getSource()==bt1){
txtDisplay.setText(txtDisplay.getText()+"5");
}
if(e.getSource()==bt2){
txtDisplay.setText(txtDisplay.getText()+"6");
}
if(e.getSource()==b0){
txtDisplay.setText(txtDisplay.getText()+"7");
}
if(e.getSource()==b1){
txtDisplay.setText(txtDisplay.getText()+"8");
}
if(e.getSource()==b2){
txtDisplay.setText(txtDisplay.getText()+"9");
}
if(e.getSource()==bt9){
txtDisplay.setText(txtDisplay.getText()+".");
}
if(e.getSource()==bt11){
this.n1=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
this.operator='+';
}
if(e.getSource()==bt7){
this.n1=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
this.operator='-';
}
if(e.getSource()==clear){
this.n1=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
this.operator='?';
}
if(e.getSource()==b3){
this.n1=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
this.operator='/';
}
if(e.getSource()==bt3){
this.n1=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
this.operator='*';
}
if(e.getSource()==bt10){
int answer;
if(operator=='+'){
this.n2=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
answer=n1+n2;
txtDisplay.setText(answer+"");

}
if (operator=='-'){
this.n2=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
answer=n1-n2;
txtDisplay.setText(answer+"");

}
if (operator=='/'){
this.n2=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
answer=n1/n2;
txtDisplay.setText(answer+"");


}
if (operator=='*'){
this.n2=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
answer=n1*n2;
txtDisplay.setText(answer+"");
}
if (operator=='?'){
this.n2=Integer.parseInt(txtDisplay.getText());
txtDisplay.setText("");
txtDisplay.setText("");
}

}
}

public static void main (String args[]){
MyFrame my=new MyFrame();
my.setTitle("Calculator");
   my.setLocationRelativeTo(null);
   my.setDefaultCloseOperation(3);
   my.pack();
my.setVisible(true);

}
}


Preview

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
Web Analytics