Advertisement
gdog2u

Untitled

Sep 3rd, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5.  
  6. public class aa extends JFrame implements ActionListener {
  7.  
  8.     private static final long serialVersionUID = 1L;
  9.     float numA = 0;
  10.     static JTextArea ans = new JTextArea(1,15);
  11.     static JMenuBar menuB = new JMenuBar();
  12.     static JMenu menu = new JMenu("File");
  13.     JMenuItem graph = new JMenuItem("Graph");
  14.    
  15.     JButton clr = new JButton("clr");
  16.     JButton sqr = new JButton("^2");
  17.     JButton pow = new JButton("^x");
  18.     JButton pnB = new JButton("+/-");
  19.     JButton deci = new JButton(".");
  20.    
  21.     JButton addB = new JButton("+");
  22.     JButton subB = new JButton("-");
  23.     JButton mulB = new JButton("*");
  24.     JButton divB = new JButton("%");
  25.     JButton equB = new JButton("=");
  26.    
  27.     JButton key1 = new JButton("1");
  28.     JButton key2 = new JButton("2");
  29.     JButton key3 = new JButton("3");
  30.     JButton key4 = new JButton("4");
  31.     JButton key5 = new JButton("5");
  32.     JButton key6 = new JButton("6");
  33.     JButton key7 = new JButton("7");
  34.     JButton key8 = new JButton("8");
  35.     JButton key9 = new JButton("9");
  36.     JButton key0 = new JButton("0");
  37.    
  38.     float x = 0;
  39.     float y = 0;
  40.    
  41.     String operator;
  42.     String lastP;
  43.    
  44.     public static void main(String[] args) {
  45.         aa app =new aa();
  46.         app.setTitle("Calculator");
  47.     }
  48.    
  49.     public aa(){
  50.         this.setJMenuBar(menuB);
  51.             menuB.add(menu);
  52.                 menu.add(graph);
  53.                     graph.addActionListener(this);
  54.         this.setVisible(true);
  55.         this.setLocationRelativeTo(null);
  56.         this.setSize(230, 240);
  57.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  58.         //JPanel content = new JPanel();
  59.         this.setLayout(new FlowLayout());
  60.         this.add(ans);
  61.             ans.setEditable(false);
  62.            
  63.         this.add(clr);
  64.             clr.addActionListener(this);
  65.         this.add(sqr);
  66.             sqr.addActionListener(this);
  67.         this.add(pow);
  68.             pow.addActionListener(this);
  69.         this.add(pnB);
  70.             pnB.addActionListener(this);
  71.        
  72.         this.add(key1);
  73.             key1.addActionListener(this);
  74.         this.add(key2);
  75.             key2.addActionListener(this);
  76.         this.add(key3);
  77.             key3.addActionListener(this);
  78.            
  79.             this.add(addB);
  80.             addB.addActionListener(this);
  81.            
  82.         this.add(key4);
  83.             key4.addActionListener(this);
  84.         this.add(key5);
  85.             key5.addActionListener(this);
  86.         this.add(key6);
  87.             key6.addActionListener(this);
  88.            
  89.             this.add(subB);
  90.             subB.addActionListener(this);
  91.            
  92.         this.add(key7);
  93.             key7.addActionListener(this);
  94.         this.add(key8);
  95.             key8.addActionListener(this);
  96.         this.add(key9);
  97.             key9.addActionListener(this);
  98.            
  99.             this.add(mulB);
  100.             mulB.addActionListener(this);
  101.            
  102.         this.add(key0);
  103.             key0.addActionListener(this);
  104.         this.add(deci);
  105.             deci.addActionListener(this);
  106.            
  107.             this.add(equB);
  108.                 equB.addActionListener(this);
  109.             this.add(divB);
  110.                 divB.addActionListener(this);
  111.            
  112.     }
  113.  
  114.     public void actionPerformed(ActionEvent e) {
  115.         if(e.getSource() == this.key1){
  116.             if(lastP == "P"){
  117.                 ans.setText(" ");
  118.                 operator="";
  119.             }
  120.             ans.append("1");
  121.             lastP = "1";
  122.         }
  123.         if(e.getSource() == this.key2){
  124.             if(lastP == "P"){
  125.                 ans.setText("");
  126.                 operator="";
  127.             }
  128.             ans.append("2");
  129.             lastP = "2";
  130.         }
  131.         if(e.getSource() == this.key3){
  132.             if(lastP == "P"){
  133.                 ans.setText("");
  134.                 operator="";
  135.             }
  136.             ans.append("3");
  137.             lastP = "3";
  138.         }
  139.         if(e.getSource() == this.key4){
  140.             if(lastP == "P"){
  141.                 ans.setText("");
  142.                 operator="";
  143.             }
  144.             ans.append("4");
  145.             lastP = "4";
  146.         }
  147.         if(e.getSource() == this.key5){
  148.             if(lastP == "P"){
  149.                 ans.setText("");
  150.                 operator="";
  151.             }
  152.             ans.append("5");
  153.             lastP = "5";
  154.         }
  155.         if(e.getSource() == this.key6){
  156.             if(lastP == "P"){
  157.                 ans.setText("");
  158.                 operator="";
  159.             }
  160.             ans.append("6");
  161.             lastP = "6";
  162.         }
  163.         if(e.getSource() == this.key7){
  164.             if(lastP == "P"){
  165.                 ans.setText("");
  166.                 operator="";
  167.             }
  168.             ans.append("7");
  169.             lastP = "7";
  170.         }
  171.         if(e.getSource() == this.key8){
  172.             if(lastP == "P"){
  173.                 ans.setText("");
  174.                 operator="";
  175.             }
  176.             ans.append("8");
  177.             lastP = "8";
  178.         }
  179.         if(e.getSource() == this.key9){
  180.             if(lastP == "P"){
  181.                 ans.setText("");
  182.                 operator="";
  183.             }
  184.             ans.append("9");
  185.             lastP = "9";
  186.         }
  187.         if(e.getSource() == this.key0){
  188.             if(lastP == "P"){
  189.                 ans.setText("");
  190.                 operator="";
  191.             }
  192.             ans.append("0");
  193.             lastP = "0";
  194.         }
  195.         if(e.getSource() == this.addB){
  196.             x =  Float.parseFloat(ans.getText());
  197.             ans.setText("");
  198.             operator = "+";
  199.             lastP = "+";
  200.         }
  201.         if(e.getSource() == this.subB){
  202.             x =  Float.parseFloat(ans.getText());
  203.             ans.setText("");
  204.             operator = "-";
  205.             lastP = "-";
  206.         }
  207.         if(e.getSource() == this.mulB){
  208.             x =  Float.parseFloat(ans.getText());
  209.             ans.setText("");
  210.             operator = "*";
  211.             lastP = "*";
  212.         }
  213.         if(e.getSource() == this.divB){
  214.             x =  Float.parseFloat(ans.getText());
  215.             ans.setText("");
  216.             operator = "/";
  217.             lastP = "/";
  218.         }
  219.         if(e.getSource() == this.pow){
  220.             x = Float.parseFloat(ans.getText());
  221.             ans.setText("");
  222.             operator = "^";
  223.             lastP = "^";
  224.         }
  225.         if(e.getSource() == this.equB){
  226.             lastP = "P";
  227.             if(operator == "+"){
  228.                 y = Float.parseFloat(ans.getText());
  229.                 numA = x+y;
  230.                 ans.setText(numA+"");
  231.             }
  232.             if(operator == "-"){
  233.                 y =Float.parseFloat(ans.getText());
  234.                 numA = x-y;
  235.                 ans.setText(numA+"");
  236.             }
  237.             if(operator == "*"){
  238.                 y =Float.parseFloat(ans.getText());
  239.                 numA = x*y;
  240.                 ans.setText(numA+"");
  241.             }
  242.             if(operator == "/"){
  243.                 y = Float.parseFloat(ans.getText());
  244.                 numA = x/y;
  245.                 ans.setText(numA+"");
  246.             }
  247.             if(operator == "^"){
  248.                 y = Float.parseFloat(ans.getText());
  249.                 numA = (float) Math.pow(x, y);
  250.                 ans.setText(numA+"");
  251.             }
  252.         }
  253.         if(e.getSource() == this.clr){
  254.             ans.setText("");
  255.             operator="";
  256.             lastP = "c";
  257.         }
  258.         if(e.getSource() == this.sqr){
  259.             x = Float.parseFloat(ans.getText());
  260.             numA = x*x;
  261.             ans.setText(numA+"");
  262.             lastP = "^2";
  263.         }
  264.         if(e.getSource() == this.deci){
  265.             if(lastP == "P"){
  266.                 ans.setText("");
  267.                 operator="";
  268.             }
  269.             ans.append(".");
  270.             lastP = "d";
  271.         }
  272.         if(e.getSource() == this.pnB){
  273.             x = Float.parseFloat(ans.getText());
  274.             numA = x * -1;
  275.             ans.setText(numA+"");
  276.         }
  277.         if(e.getSource() == this.graph){
  278.             //JOptionPane.showMessageDialog(null, "It worked");
  279.             ab grapher = new ab();
  280.         }
  281.     }
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement