Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 KB | None | 0 0
  1. import com.sun.deploy.panel.JHighDPITable;
  2.  
  3. import javax.swing.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class Main extends JFrame implements ActionListener{
  8.     JButton num1,
  9.             num2,
  10.             num3,
  11.             num4,
  12.             num5,
  13.             num6,
  14.             num7,
  15.             num8,
  16.             num9,
  17.             num0,
  18.             plus,
  19.             minus,
  20.             mnozenie,
  21.             dzielnie,
  22.             wynik,
  23.             M1,
  24.             M2,
  25.             C,
  26.             CE;
  27.     JLabel book;
  28.     double liczba1,
  29.         liczba2,
  30.         pamiec;
  31.     int dzialanie = 0;
  32.  
  33.     String historia = new String("");
  34.  
  35.     void dodajZnak(String liczba)
  36.     {
  37.         String napis = book.getText();
  38.         napis = napis + liczba;
  39.         book.setText(napis);
  40.     }
  41.     public Main()
  42.     {
  43.         setSize( 600, 400);
  44.         setTitle("Kalkulator arytmetyczny");
  45.         setLayout(null);
  46.         setResizable(false);
  47.  
  48.         book = new JLabel("");
  49.         book.setBounds(150,40,292,50);
  50.         add(book);
  51.  
  52.         num1 = new JButton("1");
  53.         num1.setBounds(150,220,50,50);
  54.         add(num1);
  55.         num1.addActionListener(this);
  56.  
  57.         num2 = new JButton("2");
  58.         num2.setBounds(210,220,50,50);
  59.         add(num2);
  60.         num2.addActionListener(this);
  61.  
  62.         num3 = new JButton("3");
  63.         num3.setBounds(270,220,50,50);
  64.         add(num3);
  65.         num3.addActionListener(this);
  66.  
  67.         num4 = new JButton("4");
  68.         num4.setBounds(150,160,50,50);
  69.         add(num4);
  70.         num4.addActionListener(this);
  71.  
  72.         num5 = new JButton("5");
  73.         num5.setBounds(210,160,50,50);
  74.         add(num5);
  75.         num5.addActionListener(this);
  76.  
  77.         num6 = new JButton("6");
  78.         num6.setBounds(270,160,50,50);
  79.         add(num6);
  80.         num6.addActionListener(this);
  81.  
  82.         num7 = new JButton("7");
  83.         num7.setBounds(150,100,50,50);
  84.         add(num7);
  85.         num7.addActionListener(this);
  86.  
  87.         num8 = new JButton("8");
  88.         num8.setBounds(210,100,50,50);
  89.         add(num8);
  90.         num8.addActionListener(this);
  91.  
  92.         num9 = new JButton("9");
  93.         num9.setBounds(270,100,50,50);
  94.         add(num9);
  95.         num9.addActionListener(this);
  96.  
  97.         num0 = new JButton("0");
  98.         num0.setBounds(150,280,110,50);
  99.         add(num0);
  100.         num0.addActionListener(this);
  101.  
  102.         plus = new JButton("+");
  103.         plus.setBounds(270,280,50,50);
  104.         add(plus);
  105.         plus.addActionListener(this);
  106.  
  107.         mnozenie = new JButton("*");
  108.         mnozenie.setBounds(330,100,50,50);
  109.         add(mnozenie);
  110.         mnozenie.addActionListener(this);
  111.  
  112.         dzielnie = new JButton("/");
  113.         dzielnie.setBounds(330,160,50,50);
  114.         add(dzielnie);
  115.         dzielnie.addActionListener(this);
  116.  
  117.         minus = new JButton("-");
  118.         minus.setBounds(330,220,50,50);
  119.         add(minus);
  120.         minus.addActionListener(this);
  121.  
  122.         wynik = new JButton("=");
  123.         wynik.setBounds(330,280,50,50);
  124.         add(wynik);
  125.         wynik.addActionListener(this);
  126.  
  127.         M1 = new JButton("M+");
  128.         M1.setBounds(390,100,51,50);
  129.         add(M1);
  130.         M1.addActionListener(this);
  131.  
  132.         M2 = new JButton("M-");
  133.         M2.setBounds(390,160,51,50);
  134.         add(M2);
  135.         M2.addActionListener(this);
  136.  
  137.         C = new JButton("C");
  138.         C.setBounds(390,220,51,50);
  139.         add(C);
  140.         C.addActionListener(this);
  141.  
  142.         CE = new JButton("CE");
  143.         CE.setBounds(390,280,51,50);
  144.         add(CE);
  145.         CE.addActionListener(this);
  146.     }
  147.     public void actionPerformed(ActionEvent e)
  148.     {
  149.         Object event = e.getSource();
  150.         if(event==num1)
  151.         {
  152.             dodajZnak("1");
  153.         }else if(event==num2)
  154.         {
  155.             dodajZnak("2");
  156.         }else if(event==num3)
  157.         {
  158.             dodajZnak("3");
  159.         }else if(event==num4)
  160.         {
  161.             dodajZnak("4");
  162.         }else if(event==num5)
  163.         {
  164.             dodajZnak("5");
  165.         }else if(event==num6)
  166.         {
  167.             dodajZnak("6");
  168.         }else if(event==num7)
  169.         {
  170.             dodajZnak("7");
  171.         }else if(event==num8)
  172.         {
  173.             dodajZnak("8");
  174.         }else if(event==num9)
  175.         {
  176.             dodajZnak("9");
  177.         }else if(event==num0)
  178.         {
  179.             dodajZnak("0");
  180.         }else if(event==plus)
  181.         {
  182.             liczba1 = Double.parseDouble(book.getText());
  183.             book.setText("");
  184.             dzialanie = 1;
  185.  
  186.         }else if(event==minus)
  187.         {
  188.             liczba1 = Double.parseDouble(book.getText());
  189.             book.setText("");
  190.             dzialanie = 2;
  191.         }else if(event==mnozenie)
  192.         {
  193.             liczba1 = Double.parseDouble(book.getText());
  194.             book.setText("");
  195.             dzialanie = 3;
  196.         }else if(event==dzielnie)
  197.         {
  198.             liczba1 = Double.parseDouble(book.getText());
  199.             book.setText("");
  200.             dzialanie = 4;
  201.         }else if(event==wynik) {
  202.             liczba2 = Double.parseDouble(book.getText());
  203.             if (dzialanie == 1) {
  204.                 double is = liczba1 + liczba2;
  205.                 book.setText(String.valueOf(is));
  206.             } else if (dzialanie == 2) {
  207.                 double is = liczba1 - liczba2;
  208.                 book.setText(String.valueOf(is));
  209.             } else if (dzialanie == 3) {
  210.                 double is = liczba1 * liczba2;
  211.                 book.setText(String.valueOf(is));
  212.             } else if (dzialanie == 4) {
  213.                 double is = liczba1 / liczba2;
  214.                 book.setText(String.valueOf(is));
  215.             }
  216.         }else if(event==M1)
  217.         {
  218.             double liczba = Double.parseDouble(book.getText());
  219.             pamiec = liczba;
  220.         }else if(event==M2)
  221.         {
  222.             book.setText(String.valueOf(pamiec));
  223.         }else if(event==C)
  224.         {
  225.             book.setText("");
  226.             dzialanie = 0;
  227.         }else if(event==CE)
  228.         {
  229.             String napis = book.getText();
  230.             napis = napis.substring(0,(napis.length()-1));
  231.             book.setText(napis);
  232.         }
  233.     }
  234.     public static void main(String[] args) {
  235.     // write your code here
  236.         Main window = new Main();
  237.         window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  238.         window.setVisible(true);
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement