Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.deploy.panel.JHighDPITable;
- import javax.swing.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Main extends JFrame implements ActionListener{
- JButton num1,
- num2,
- num3,
- num4,
- num5,
- num6,
- num7,
- num8,
- num9,
- num0,
- plus,
- minus,
- mnozenie,
- dzielnie,
- wynik,
- M1,
- M2,
- C,
- CE;
- JLabel book;
- double liczba1,
- liczba2,
- pamiec;
- int dzialanie = 0;
- String historia = new String("");
- void dodajZnak(String liczba)
- {
- String napis = book.getText();
- napis = napis + liczba;
- book.setText(napis);
- }
- public Main()
- {
- setSize( 600, 400);
- setTitle("Kalkulator arytmetyczny");
- setLayout(null);
- setResizable(false);
- book = new JLabel("");
- book.setBounds(150,40,292,50);
- add(book);
- num1 = new JButton("1");
- num1.setBounds(150,220,50,50);
- add(num1);
- num1.addActionListener(this);
- num2 = new JButton("2");
- num2.setBounds(210,220,50,50);
- add(num2);
- num2.addActionListener(this);
- num3 = new JButton("3");
- num3.setBounds(270,220,50,50);
- add(num3);
- num3.addActionListener(this);
- num4 = new JButton("4");
- num4.setBounds(150,160,50,50);
- add(num4);
- num4.addActionListener(this);
- num5 = new JButton("5");
- num5.setBounds(210,160,50,50);
- add(num5);
- num5.addActionListener(this);
- num6 = new JButton("6");
- num6.setBounds(270,160,50,50);
- add(num6);
- num6.addActionListener(this);
- num7 = new JButton("7");
- num7.setBounds(150,100,50,50);
- add(num7);
- num7.addActionListener(this);
- num8 = new JButton("8");
- num8.setBounds(210,100,50,50);
- add(num8);
- num8.addActionListener(this);
- num9 = new JButton("9");
- num9.setBounds(270,100,50,50);
- add(num9);
- num9.addActionListener(this);
- num0 = new JButton("0");
- num0.setBounds(150,280,110,50);
- add(num0);
- num0.addActionListener(this);
- plus = new JButton("+");
- plus.setBounds(270,280,50,50);
- add(plus);
- plus.addActionListener(this);
- mnozenie = new JButton("*");
- mnozenie.setBounds(330,100,50,50);
- add(mnozenie);
- mnozenie.addActionListener(this);
- dzielnie = new JButton("/");
- dzielnie.setBounds(330,160,50,50);
- add(dzielnie);
- dzielnie.addActionListener(this);
- minus = new JButton("-");
- minus.setBounds(330,220,50,50);
- add(minus);
- minus.addActionListener(this);
- wynik = new JButton("=");
- wynik.setBounds(330,280,50,50);
- add(wynik);
- wynik.addActionListener(this);
- M1 = new JButton("M+");
- M1.setBounds(390,100,51,50);
- add(M1);
- M1.addActionListener(this);
- M2 = new JButton("M-");
- M2.setBounds(390,160,51,50);
- add(M2);
- M2.addActionListener(this);
- C = new JButton("C");
- C.setBounds(390,220,51,50);
- add(C);
- C.addActionListener(this);
- CE = new JButton("CE");
- CE.setBounds(390,280,51,50);
- add(CE);
- CE.addActionListener(this);
- }
- public void actionPerformed(ActionEvent e)
- {
- Object event = e.getSource();
- if(event==num1)
- {
- dodajZnak("1");
- }else if(event==num2)
- {
- dodajZnak("2");
- }else if(event==num3)
- {
- dodajZnak("3");
- }else if(event==num4)
- {
- dodajZnak("4");
- }else if(event==num5)
- {
- dodajZnak("5");
- }else if(event==num6)
- {
- dodajZnak("6");
- }else if(event==num7)
- {
- dodajZnak("7");
- }else if(event==num8)
- {
- dodajZnak("8");
- }else if(event==num9)
- {
- dodajZnak("9");
- }else if(event==num0)
- {
- dodajZnak("0");
- }else if(event==plus)
- {
- liczba1 = Double.parseDouble(book.getText());
- book.setText("");
- dzialanie = 1;
- }else if(event==minus)
- {
- liczba1 = Double.parseDouble(book.getText());
- book.setText("");
- dzialanie = 2;
- }else if(event==mnozenie)
- {
- liczba1 = Double.parseDouble(book.getText());
- book.setText("");
- dzialanie = 3;
- }else if(event==dzielnie)
- {
- liczba1 = Double.parseDouble(book.getText());
- book.setText("");
- dzialanie = 4;
- }else if(event==wynik) {
- liczba2 = Double.parseDouble(book.getText());
- if (dzialanie == 1) {
- double is = liczba1 + liczba2;
- book.setText(String.valueOf(is));
- } else if (dzialanie == 2) {
- double is = liczba1 - liczba2;
- book.setText(String.valueOf(is));
- } else if (dzialanie == 3) {
- double is = liczba1 * liczba2;
- book.setText(String.valueOf(is));
- } else if (dzialanie == 4) {
- double is = liczba1 / liczba2;
- book.setText(String.valueOf(is));
- }
- }else if(event==M1)
- {
- double liczba = Double.parseDouble(book.getText());
- pamiec = liczba;
- }else if(event==M2)
- {
- book.setText(String.valueOf(pamiec));
- }else if(event==C)
- {
- book.setText("");
- dzialanie = 0;
- }else if(event==CE)
- {
- String napis = book.getText();
- napis = napis.substring(0,(napis.length()-1));
- book.setText(napis);
- }
- }
- public static void main(String[] args) {
- // write your code here
- Main window = new Main();
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- window.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement