Facebook
From Mustard Lizard, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 269
  1. import java.awt.Color;
  2. import java.awt.GridLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5.  
  6. import javax.swing.BorderFactory;
  7. import javax.swing.JButton;
  8. import javax.swing.JFrame;
  9. import javax.swing.JPanel;
  10. import javax.swing.JTextArea;
  11.  
  12. public class Kalkulator
  13. {
  14.     public static void main(String[] args)
  15.     {
  16.         new Okno();
  17.     }
  18. }
  19. class Okno extends JFrame implements ActionListener
  20. //dziedziczymy po iframe, dzięki temu możemy przesłaniać wszystkie jej metody
  21. //implementujemy interfejs actionlistener, ktory da nam niezbedna metode
  22. {
  23.     JButton[] numer = new JButton[10]; // 10 klawiszy od 0 do 10
  24.     JButton[] operatory = new JButton[6];
  25.  
  26.     JPanel panel_numerow = new JPanel(new GridLayout(4,3)); //jpanel to kontener na kontrolki a gridlayout to sposob w jaki maja byc wyswietlone
  27.     JPanel panel_operatorow = new JPanel(new GridLayout(5,1));
  28.  
  29.     JTextArea wynik = new JTextArea();
  30.     String[] bufor = {"","",""}; //pierwsza liczba,potem operator,potem liczba
  31.  
  32.     public Okno()
  33.     {
  34.         this.setSize(200,450); //domyślny rozmiar to 200x200, można rozszerzać, odwołuję się po referencji this po to by wyświetliła mi się pomocna lista komend
  35.         this.setResizable(false); //blokujemy rozszerzanie się
  36.         this.TworzOperatory();
  37.         this.TworzPanelNumerow();
  38.         this.TworzWszystko();
  39.         this.setVisible(true); //wyświetlamy okno
  40.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //domyślna operacja zamknięcia
  41.     }
  42.     private void TworzPanelNumerow()
  43.     {
  44.         for(int i = 0;i < numer.length;++i)
  45.         {
  46.             numer[i] = new JButton(String.valueOf(i)); //String.valueOf(i) konwersja int na klase String
  47.             numer[i].addActionListener(this); //ustawiamy actionlistener, czyli reakcje na akcje zwizana z tym przyciskiem
  48.             panel_numerow.add(numer[i]); //wrzucamy referencje do przycisk numer[i]
  49.         }
  50.     }
  51.     private void TworzOperatory()
  52.     {
  53.         operatory[0] = new JButton("+");
  54.         operatory[1] = new JButton("-");
  55.         operatory[2] = new JButton("*");
  56.         operatory[3] = new JButton("/");
  57.         operatory[4] = new JButton("=");
  58.         operatory[5] = new JButton("C");
  59.         for(JButton operator : operatory) //przyklad petli foreach w javie
  60.         {
  61.             operator.addActionListener(this); //dodajemy do referencji actionlistener
  62.             this.panel_operatorow.add(operator);
  63.         }
  64.     }
  65.     private void TworzWszystko()
  66.     {
  67.         this.setLayout(null); //ustawiamy wartosc panelu na null, poniewaz z wlaczonym panelem nie mozemy korzystac z bounds kontrolek
  68.  
  69.         this.panel_numerow.setBounds(30, 80, 130, 130);
  70.         this.panel_operatorow.setBounds(30,220,130,130);
  71.  
  72.         this.wynik.setBorder(BorderFactory.createLineBorder(Color.black)); //tworzymy obramowanie dla pola tekstowego
  73.         this.wynik.setBounds(0, 0, this.getWidth(), 35);//ustawiamy boundsy dla pola tekstowego
  74.  
  75.         this.add(this.wynik);
  76.         this.add(this.panel_operatorow);
  77.         this.add(this.panel_numerow);
  78.     }
  79.     private double PrzeliczBufor(String operator) //wylicza wyrazenie z 2 argumentow
  80.     {
  81.         if(!bufor[0].equals("") && !bufor[2].equals(""))
  82.         {
  83.             if(operator.equals("+"))
  84.             {
  85.                 return Double.valueOf(bufor[0])+Double.valueOf(bufor[2]);
  86.             }else if(operator.equals("-"))
  87.             {
  88.                 return Double.valueOf(bufor[0])-Double.valueOf(bufor[2]);
  89.             }else if(operator.equals("*"))
  90.             {
  91.                 return Double.valueOf(bufor[0])*Double.valueOf(bufor[2]);
  92.             }else if(operator.equals("/"))
  93.             {
  94.                 return Double.valueOf(bufor[0])/Double.valueOf(bufor[2]);
  95.             }else return -1;
  96.         }else return -1;
  97.     }
  98.     private void Czysc()
  99.     {
  100.         bufor[0] = bufor[1] = bufor[2] = ""; //tak tez mozna :-)
  101.         this.wynik.setText("");
  102.     }
  103.     //interfejsy to inaczej klasy abstrackcyjne i nie maja definicji metod, tak wiec musimy obowiazkowo umiescic je w kodzie i przeslonic
  104.     public void ObliczIWyswietl()
  105.     {
  106.         this.wynik.setText(String.valueOf(this.PrzeliczBufor(bufor[1])));
  107.         this.bufor[0] = this.wynik.getText();
  108.         this.bufor[1] = "";
  109.         this.bufor[2] = "";
  110.     }
  111.     @Override
  112.     public void actionPerformed(ActionEvent e) {
  113.         for(JButton przycisk : this.operatory)
  114.         {
  115.             if(e.getSource().equals(przycisk))
  116.             {
  117.                 if(!przycisk.getText().equals("=") && !przycisk.getText().equals("C"))
  118.                 {
  119.                     this.wynik.setText("");
  120.                     bufor[1] = przycisk.getText();
  121.                 }else if(przycisk.getText().equals("="))
  122.                 {
  123.                     this.ObliczIWyswietl();
  124.                 }else
  125.                 {
  126.                     this.Czysc();
  127.                 }
  128.                 break;
  129.             }
  130.         }
  131.         for(JButton przycisk : this.numer)
  132.         {
  133.             if(e.getSource().equals(przycisk))
  134.             {
  135.                 if(bufor[1].equals(""))
  136.                 {
  137.                     bufor[0] = bufor[0]+przycisk.getText();
  138.                     this.wynik.setText(bufor[0]);
  139.                     break;
  140.                 }else
  141.                 {
  142.                     bufor[2] = bufor[2]+przycisk.getText();
  143.                     this.wynik.setText(bufor[2]);
  144.                 }
  145.             }
  146.         }
  147.     }
  148. }