Nalakava

GUI CALC

Apr 10th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.71 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.event.*;
  3. import java.awt.*;
  4.  
  5. public class Frame extends JFrame implements ActionListener
  6. {
  7.    JLayeredPane layeredPane;
  8.    JButton button1;
  9.    JButton button2;
  10.    JButton button3;
  11.    JButton button4;
  12.    JButton button5;
  13.    JButton button6;
  14.    JButton button7;
  15.    JButton button8;
  16.    JButton button9;
  17.    String result = "0";
  18.    JLabel numberGrid;
  19.    JLabel resultLabel;
  20.    JLabel operationsLabel;
  21.    JButton plus;
  22.    JButton minus;
  23.    JButton times;
  24.    JButton division;
  25.    JButton equals;
  26.    JButton log;
  27.    JButton ac;
  28.    JLabel equalsLabel;
  29.    Font font;
  30.    int timesClicked = 0;
  31.    double a;
  32.    double b;
  33.    double answer = 0;
  34.    int operation;
  35.    
  36.    public Frame()
  37.    {
  38.       JFrame frame = new JFrame();
  39.      
  40.       frame.setSize(1000, 825);
  41.       frame.setLocationRelativeTo(null);
  42.       frame.setResizable(false);
  43.       frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
  44.      
  45.       font = new Font("SANS_SERIF", Font.PLAIN, 24);
  46.      
  47.       layeredPane = frame.getLayeredPane();
  48.       layeredPane.setVisible(true);
  49.       layeredPane.setLayout(null);
  50.      
  51.       numberGrid = new JLabel();
  52.       numberGrid.setBorder(BorderFactory.createLineBorder((Color.black), 12));
  53.       numberGrid.setBounds(0,200,600,600);
  54.       numberGrid.setLayout(new GridLayout(3,3));
  55.       layeredPane.add(numberGrid, 0);
  56.      
  57.       resultLabel = new JLabel(result);
  58.       resultLabel.setBorder(BorderFactory.createLineBorder((Color.white), 12));
  59.       resultLabel.setBounds(0,0,1000,200);
  60.       resultLabel.setOpaque(true);
  61.       resultLabel.setFont(font);
  62.       layeredPane.add(resultLabel);
  63.      
  64.       operationsLabel = new JLabel();
  65.       operationsLabel.setBorder(BorderFactory.createLineBorder((Color.black), 12));
  66.       operationsLabel.setBounds(600,200,400,400);
  67.       operationsLabel.setBackground(Color.gray);
  68.       operationsLabel.setOpaque(true);
  69.       operationsLabel.setLayout(new GridLayout(2, 2));
  70.       layeredPane.add(operationsLabel);
  71.      
  72.       equalsLabel = new JLabel();
  73.       equalsLabel.setOpaque(true);
  74.       equalsLabel.setBounds(600,600,400,200);
  75.       equalsLabel.setLayout(new BorderLayout());
  76.       equalsLabel.setBorder(BorderFactory.createLineBorder((Color.black), 12));
  77.       layeredPane.add(equalsLabel);
  78.      
  79.       button1 = new JButton("1");
  80.       button1.addActionListener(this);
  81.       button1.setFont(font);
  82.       numberGrid.add(button1);
  83.      
  84.       button2 = new JButton("2");
  85.       button2.addActionListener(this);
  86.       button2.setFont(font);
  87.       numberGrid.add(button2);
  88.      
  89.       button3 = new JButton("3");
  90.       button3.addActionListener(this);
  91.       button3.setFont(font);
  92.       numberGrid.add(button3);
  93.      
  94.       button4 = new JButton("4");
  95.       button4.addActionListener(this);
  96.       button4.setFont(font);
  97.       numberGrid.add(button4);
  98.      
  99.       button5 = new JButton("5");
  100.       button5.addActionListener(this);
  101.       button5.setFont(font);
  102.       numberGrid.add(button5);
  103.      
  104.       button6 = new JButton("6");
  105.       button6.addActionListener(this);
  106.       button6.setFont(font);
  107.       numberGrid.add(button6);
  108.      
  109.       button7 = new JButton("7");
  110.       button7.addActionListener(this);
  111.       button7.setFont(font);
  112.       numberGrid.add(button7);
  113.      
  114.       button8 = new JButton("8");
  115.       button8.addActionListener(this);
  116.       button8.setFont(font);
  117.       numberGrid.add(button8);
  118.      
  119.       button9 = new JButton("9");
  120.       button9.addActionListener(this);
  121.       button9.setFont(font);
  122.       numberGrid.add(button9);
  123.      
  124.       plus = new JButton("+");
  125.       plus.addActionListener(this);
  126.       plus.setFont(font);
  127.       operationsLabel.add(plus);
  128.      
  129.       minus = new JButton("-");
  130.       minus.addActionListener(this);
  131.       minus.setFont(font);
  132.       operationsLabel.add(minus);
  133.      
  134.       times = new JButton("X");
  135.       times.addActionListener(this);
  136.       times.setFont(font);
  137.       operationsLabel.add(times);
  138.      
  139.       division = new JButton("/");
  140.       division.addActionListener(this);
  141.       division.setFont(font);
  142.       operationsLabel.add(division);
  143.      
  144.       log = new JButton("log(a|b)");
  145.       log.addActionListener(this);
  146.       log.setFont(font);
  147.       operationsLabel.add(log);
  148.      
  149.       ac = new JButton("AC");
  150.       ac.addActionListener(this);
  151.       ac.setFont(font);
  152.       operationsLabel.add(ac);
  153.      
  154.       equals = new JButton("=");
  155.       equals.addActionListener(this);
  156.       equals.setFont(font);
  157.       equalsLabel.add(equals);
  158.      
  159.       frame.setVisible(true);
  160.    }
  161.    
  162.    public void actionPerformed(ActionEvent e)
  163.    {
  164.       if(e.getSource() instanceof JButton)
  165.       {
  166.          JButton tempButton = (JButton)e.getSource();
  167.          
  168.          if(!(tempButton == plus || tempButton == minus || tempButton == division || tempButton == times || tempButton == equals || tempButton == log || tempButton == ac))
  169.          {
  170.             resultLabel.setText(tempButton.getText());
  171.            
  172.             if(timesClicked == 0)
  173.             {
  174.                a = (double)Integer.parseInt(tempButton.getText());
  175.                timesClicked++;
  176.             }
  177.            
  178.             else if(timesClicked >= 1)
  179.             {
  180.                b = (double)Integer.parseInt(tempButton.getText());
  181.                timesClicked++;
  182.             }
  183.          }
  184.          
  185.          if(tempButton == plus || tempButton == minus || tempButton == times || tempButton == division || tempButton == log || tempButton == ac)
  186.          {
  187.             switch(tempButton.getText())
  188.             {
  189.                case "+": operation = 1;
  190.                break;
  191.                case "-": operation = 2;
  192.                break;
  193.                case "X": operation = 3;
  194.                break;
  195.                case "/": operation = 4;
  196.                break;
  197.                case "log(a|b)": operation = 5;
  198.                break;
  199.                default: break;
  200.             }
  201.          }
  202.          
  203.          if(tempButton == equals)
  204.          {
  205.             switch(operation)
  206.             {
  207.                case 1: answer = a + b;
  208.                break;
  209.                case 2: answer = a - b;
  210.                break;
  211.                case 3: answer = a*b;
  212.                break;
  213.                case 4: answer = a/b;
  214.                break;
  215.                case 5: answer = (Math.log(a))/(Math.log(b));
  216.                break;
  217.             }
  218.            
  219.             resultLabel.setText("" + answer);
  220.            
  221.             a = answer;
  222.             b = 0;
  223.          }
  224.          
  225.          if(tempButton == ac)
  226.          {
  227.             timesClicked = 0;
  228.             answer = 0;
  229.             a = 0;
  230.             resultLabel.setText("Reset");
  231.          }
  232.       }
  233.    }
  234. }
Add Comment
Please, Sign In to add comment