Advertisement
RepNazar

Untitled

Dec 8th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. public class kalkulat extends JFrame {
  5.     JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19;
  6.     JLabel l1;
  7.     JTextField t1;
  8.     int a,b,c;
  9.     String s;
  10.     eHandler handler = new eHandler();
  11.     public static void main(String args[]){
  12.         kalkulat kalk = new kalkulat("Калькулятор");
  13.         kalk.setVisible(true);
  14.         kalk.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15.         kalk.setSize(224,224);
  16.         kalk.setResizable(false);
  17.         kalk.setLocationRelativeTo(null);
  18. }
  19.     public kalkulat(String s){
  20.         super(s);
  21.         setLayout(new FlowLayout());
  22.         l1= new JLabel("                        ");
  23.         b1 = new JButton("1");
  24.         b2 = new JButton("2");
  25.         b3 = new JButton("3");
  26.         b4 = new JButton("4");
  27.         b5 = new JButton("5");
  28.         b6 = new JButton("6");
  29.         b7 = new JButton("7");
  30.         b8 = new JButton("8");
  31.         b9 = new JButton("9");
  32.         b0 = new JButton("0");
  33.         b10 = new JButton("+");
  34.         b11 = new JButton("     -      ");
  35.         b12 = new JButton("     *      ");
  36.         b13 = new JButton("     /      ");
  37.         b14 = new JButton("±");
  38.         b15 = new JButton("Sqr");
  39.         b16 = new JButton("Sqrt");
  40.         b17 = new JButton("c");
  41.         b18 = new JButton("<|");
  42.         b19 = new JButton("     =     ");
  43.         t1 = new JTextField(19);
  44.         add(t1);
  45.         add(b16);
  46.         add(b15);
  47.         add(b17);
  48.         add(b18);
  49.         add(b7);
  50.         add(b8);
  51.         add(b9);
  52.         add(b13);
  53.         add(b4);
  54.         add(b5);
  55.         add(b6);
  56.         add(b12);
  57.         add(b1);
  58.         add(b2);
  59.         add(b3);
  60.         add(b11);
  61.         add(b10);
  62.         add(b0);
  63.         add(b14);
  64.         add(b19);
  65.         add(l1);
  66.         b0.addActionListener(handler);
  67.         b1.addActionListener(handler);
  68.         b2.addActionListener(handler);
  69.         b3.addActionListener(handler);
  70.         b4.addActionListener(handler);
  71.         b5.addActionListener(handler);
  72.         b6.addActionListener(handler);
  73.         b7.addActionListener(handler);
  74.         b8.addActionListener(handler);
  75.         b9.addActionListener(handler);
  76.         b10.addActionListener(handler);
  77.         b11.addActionListener(handler);
  78.         b12.addActionListener(handler);
  79.         b13.addActionListener(handler);
  80.         b14.addActionListener(handler);
  81.         b15.addActionListener(handler);
  82.         b16.addActionListener(handler);
  83.         b17.addActionListener(handler);
  84.         b18.addActionListener(handler);
  85.         b19.addActionListener(handler);
  86.        
  87.     }
  88.    
  89.     public class eHandler implements ActionListener{
  90.         public void actionPerformed(ActionEvent e) {
  91.             try{
  92.             if(e.getSource()==b0){
  93.             t1.setText(t1.getText() + "0");
  94.             }
  95.             if(e.getSource()==b1){
  96.             t1.setText(t1.getText() + "1");
  97.             }
  98.             if(e.getSource()==b2){     
  99.             t1.setText(t1.getText() + "2");
  100.             }
  101.             if(e.getSource()==b3){
  102.             t1.setText(t1.getText() + "3");
  103.             }
  104.             if(e.getSource()==b4){
  105.             t1.setText(t1.getText() + "4");
  106.             }
  107.             if(e.getSource()==b5){
  108.             t1.setText(t1.getText() + "5");
  109.             }
  110.             if(e.getSource()==b6){
  111.             t1.setText(t1.getText() + "6");
  112.             }
  113.             if(e.getSource()==b7){
  114.             t1.setText(t1.getText() + "7");
  115.             }
  116.             if(e.getSource()==b8){
  117.             t1.setText(t1.getText() + "8");
  118.             }
  119.             if(e.getSource()==b9){
  120.             t1.setText(t1.getText() + "9");
  121.             }
  122.             if(e.getSource()==b19){
  123.                
  124.             if(l1.getText()=="+"){
  125.                 b = Integer.parseInt(t1.getText());
  126.                 c=a+b;
  127.                 a=0;
  128.                 b=0;
  129.                 t1.setText(""+ c);
  130.                 l1.setText("");
  131.             }
  132.             if(l1.getText()=="-"){
  133.                 b = Integer.parseInt(t1.getText());
  134.                 c=a-b;
  135.                 a=0;
  136.                 b=0;
  137.                 t1.setText(""+ c);
  138.                 l1.setText("");
  139.             }
  140.             if(l1.getText()=="*"){
  141.                 b = Integer.parseInt(t1.getText());
  142.                 c=a*b;
  143.                 a=0;
  144.                 b=0;
  145.                 t1.setText(""+ c);
  146.                 l1.setText("");
  147.             }
  148.             if(l1.getText()=="/"){
  149.                 b = Integer.parseInt(t1.getText());
  150.                 c=a/b;
  151.                 a=0;
  152.                 b=0;
  153.                 t1.setText(""+ c);
  154.                 l1.setText("");
  155.             }
  156.             }
  157.             if(e.getSource()==b10){
  158.                 a=Integer.parseInt(t1.getText());  
  159.                 t1.setText("");
  160.                 l1.setText("+");
  161.                 }
  162.             if(e.getSource()==b11){
  163.                 a=Integer.parseInt(t1.getText());
  164.                 t1.setText("");
  165.                 l1.setText("-");
  166.             }
  167.             if(e.getSource()==b12){
  168.                 a=Integer.parseInt(t1.getText());
  169.                 t1.setText("");
  170.                 l1.setText("*");
  171.             }
  172.             if(e.getSource()==b13){
  173.                 a=Integer.parseInt(t1.getText());
  174.                 t1.setText("");
  175.                 l1.setText("/");
  176.             }
  177.             if(e.getSource()==b14){
  178.             a=Integer.parseInt(t1.getText());  
  179.             a=-a;
  180.             t1.setText(""+a);
  181.             l1.setText("+-");
  182.             }
  183.             if(e.getSource()==b15){
  184.             a=Integer.parseInt(t1.getText());  
  185.             a=a*a;
  186.             t1.setText("" + a);
  187.             l1.setText("Sqr");
  188.             }
  189.             if(e.getSource()==b16){
  190.                 a=Integer.parseInt(t1.getText());  
  191.                 t1.setText("" + Math.sqrt(a));
  192.                 l1.setText("Sqrt");
  193.             }
  194.             if(e.getSource()==b17){
  195.                 t1.setText("");
  196.             }
  197.             if(e.getSource()==b18){
  198.                 s=t1.getText();
  199.                 s=s.substring(0, s.length()-1);
  200.                 t1.setText(""+s);  
  201.             }
  202. }catch(Exception ex){JOptionPane.showInternalMessageDialog(null,"Помилка вводу"); t1.setText("");l1.setText("");a=0;b=0;c=0;s="";    
  203. }}
  204. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement