Facebook
From Tacky Penguin, 8 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 441
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class CalculatorSwing extends JFrame implements ActionListener {
  6.  
  7.     private int rowNumber = 5;
  8.     private int buttonNumber = 19;
  9.     private int operationNumber = 4;
  10.  
  11.     JPanel[] row = new JPanel[rowNumber];
  12.     JButton[] button = new JButton[buttonNumber];
  13.     String[] buttonString = {"7", "8", "9", "+",
  14.             "4", "5", "6", "-",
  15.             "1", "2", "3", "*",
  16.             ".", "/", "C", "√",
  17.             "+/-", "=", "0"};
  18.  
  19.     int[] dimW = {300, 45, 100, 90};
  20.     int[] dimH = {35, 40};
  21.     Dimension displayDimension = new Dimension(dimW[0], dimH[0]);
  22.     Dimension regularDimension = new Dimension(dimW[1], dimH[1]);
  23.     Dimension rColumnDimension = new Dimension(dimW[2], dimH[1]);
  24.     Dimension zeroButDimension = new Dimension(dimW[3], dimH[1]);
  25.  
  26.  
  27.     boolean[] function = new boolean[operationNumber];
  28.     double[] temporary = {0,0};
  29.     JTextArea display = new JTextArea(1, 20);
  30.     Font font = new Font("Times new Roman", Font.BOLD, 14);
  31.  
  32.     CalculatorSwing() {
  33.         super("Kalkulatorek");
  34.         setDesign();
  35.         setSize(300, 240);
  36.         setResizable(true);
  37.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  38.         GridLayout grid = new GridLayout(5, 5);
  39.         setLayout(grid);
  40.  
  41.         for (int i = 0; i < 4; i++)
  42.             function[i] = false;
  43.  
  44.         FlowLayout f1 = new FlowLayout(FlowLayout.CENTER);
  45.         FlowLayout f2 = new FlowLayout(FlowLayout.CENTER, 1, 1);
  46.  
  47.         for (int i = 0; i < 5; i++)
  48.             row[i] = new JPanel();
  49.         row[0].setLayout(f1);
  50.         for (int i = 1; i < 5; i++)
  51.             row[i].setLayout(f2);
  52.  
  53.         for (int i = 0; i < buttonNumber; i++) {
  54.             button[i] = new JButton();
  55.             button[i].setText(buttonString[i]);
  56.             button[i].setFont(font);
  57.             button[i].addActionListener(this);
  58.         }
  59.  
  60.         display.setFont(font);
  61.         display.setEditable(false);
  62.         display.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  63.         display.setPreferredSize(displayDimension);
  64.  
  65.         // ustawienie szerokosci przyciskow aby je dopasowac do okienka
  66.         for (int i = 0; i < 14; i++)
  67.             button[i].setPreferredSize(regularDimension);
  68.         for (int i = 14; i < buttonNumber - 1; i++)
  69.             button[i].setPreferredSize(rColumnDimension);
  70.  
  71.         button[18].setPreferredSize(zeroButDimension);
  72.  
  73.  
  74.         setVisible(true);
  75.  
  76.         for(int i=0; i<=4; i++) add(row[i]);
  77.         row[0].add(display);
  78.  
  79.         for (int i = 0; i < 4; i++) {
  80.             row[1].add(button[i]);
  81.         }
  82.         row[1].add(button[14]);
  83.  
  84.         for (int i = 4; i < 8; i++) {
  85.             row[2].add(button[i]);
  86.         }
  87.         row[2].add(button[15]);
  88.  
  89.         for (int i = 8; i < 12; i++)
  90.         {
  91.             row[3].add(button[i]);
  92.         }
  93.         row[3].add(button[16]);
  94.  
  95.         row[4].add(button[18]);
  96.  
  97.         for (int i = 12; i < 14; i++) {
  98.             row[4].add(button[i]);
  99.         }
  100.         row[4].add(button[17]);
  101.  
  102.  
  103.     }
  104.  
  105.     public void clear() {
  106.         try {
  107.             display.setText("");
  108.             for(int i = 0; i < 4; i++)
  109.                 function[i] = false;
  110.             for(int i = 0; i < 2; i++)
  111.                 temporary[i] = 0;
  112.         } catch(NullPointerException e) {
  113.         }
  114.     }
  115.  
  116.     public final void setDesign() {
  117.         try {
  118.             UIManager.setLookAndFeel(
  119.                     "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
  120.         } catch(Exception e) {
  121.         }
  122.     }
  123.  
  124.  
  125.     public void getSqrt() {
  126.         try {
  127.             double value = Math.sqrt(Double.parseDouble(display.getText()));
  128.             display.setText(Double.toString(value));
  129.         } catch(NumberFormatException e) {
  130.         }
  131.     }
  132.  
  133.     public void getPosNeg() {
  134.         try {
  135.             double value = Double.parseDouble(display.getText());
  136.             if(value != 0) {
  137.                 value = value * (-1);
  138.                 display.setText(Double.toString(value));
  139.             }
  140.             else {
  141.             }
  142.         } catch(NumberFormatException e) {
  143.         }
  144.     }
  145.  
  146.     public void getResult() {
  147.         double result = 0;
  148.         temporary[1] = Double.parseDouble(display.getText());
  149.         String temp0 = Double.toString(temporary[0]);
  150.         String temp1 = Double.toString(temporary[1]);
  151.         try {
  152.             if(temp0.contains("-")) {
  153.                 String[] temp00 = temp0.split("-", 2);
  154.                 temporary[0] = (Double.parseDouble(temp00[1]) * -1);
  155.             }
  156.             if(temp1.contains("-")) {
  157.                 String[] temp11 = temp1.split("-", 2);
  158.                 temporary[1] = (Double.parseDouble(temp11[1]) * -1);
  159.             }
  160.         } catch(ArrayIndexOutOfBoundsException e) {
  161.         }
  162.         try {
  163.             if(function[2] == true)
  164.                 result = temporary[0] * temporary[1];
  165.             else if(function[3] == true)
  166.                 result = temporary[0] / temporary[1];
  167.             else if(function[0] == true)
  168.                 result = temporary[0] + temporary[1];
  169.             else if(function[1] == true)
  170.                 result = temporary[0] - temporary[1];
  171.             display.setText(Double.toString(result));
  172.             for(int i = 0; i < 4; i++)
  173.                 function[i] = false;
  174.         } catch(NumberFormatException e) {
  175.         }
  176.     }
  177.  
  178.     public void actionPerformed(ActionEvent ae) {
  179.         if(ae.getSource() == button[0])
  180.             display.append("7");
  181.         if(ae.getSource() == button[1])
  182.             display.append("8");
  183.         if(ae.getSource() == button[2])
  184.             display.append("9");
  185.         if(ae.getSource() == button[3]) {
  186.             temporary[0] = Double.parseDouble(display.getText());
  187.             function[0] = true;
  188.             display.setText("");
  189.         }
  190.         if(ae.getSource() == button[4])
  191.             display.append("4");
  192.         if(ae.getSource() == button[5])
  193.             display.append("5");
  194.         if(ae.getSource() == button[6])
  195.             display.append("6");
  196.         if(ae.getSource() == button[7]) {
  197.             temporary[0] = Double.parseDouble(display.getText());
  198.             function[1] = true;
  199.             display.setText("");
  200.         }
  201.         if(ae.getSource() == button[8])
  202.             display.append("1");
  203.         if(ae.getSource() == button[9])
  204.             display.append("2");
  205.         if(ae.getSource() == button[10])
  206.             display.append("3");
  207.         if(ae.getSource() == button[11]) {
  208.             temporary[0] = Double.parseDouble(display.getText());
  209.             function[2] = true;
  210.             display.setText("");
  211.         }
  212.         if(ae.getSource() == button[12])
  213.             display.append(".");
  214.         if(ae.getSource() == button[13]) {
  215.             temporary[0] = Double.parseDouble(display.getText());
  216.             function[3] = true;
  217.             display.setText("");
  218.         }
  219.         if(ae.getSource() == button[14])
  220.             clear();
  221.         if(ae.getSource() == button[15])
  222.             getSqrt();
  223.         if(ae.getSource() == button[16])
  224.             getPosNeg();
  225.         if(ae.getSource() == button[17])
  226.             getResult();
  227.         if(ae.getSource() == button[18])
  228.             display.append("0");
  229.     }
  230.  
  231.     public static void main(String[] arguments) {
  232.         CalculatorSwing c = new CalculatorSwing();
  233.     }
  234. }
  235.  
  236.  
  237.  
  238.