hikespect

omai garebel kalkulitar

Mar 5th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. package j19;
  2. import javax.swing.*;
  3.  
  4. import java.awt.*;
  5.  
  6. import java.awt.event.*;
  7.  
  8. public class j18 {
  9.     public static void main(String[] args) {
  10.  
  11.  
  12.     okno nf = new okno();
  13.  
  14.     }
  15.  
  16.     }
  17.     class okno extends JFrame {
  18.  
  19.     private JTextArea text;
  20.     private double p1 = 0, p2 = 0;
  21.     private int oper = 0;
  22.  
  23.     private void btnClick(JButton btn) {
  24. String str = btn.getText();
  25.  
  26. if (str=="Выход") System.exit(0);
  27.  
  28. else if (str=="C")
  29. {
  30.     text.setText(" ");
  31.     p1=p2=0;
  32. }
  33.  
  34.  
  35. else if (str=="=")
  36. {
  37. p2 = Integer.parseInt(text.getText());
  38. if (oper==1) text.setText(""+(p1+p2));
  39. if (oper==2) text.setText(""+(p1-p2));
  40. if (oper==3) text.setText(""+(p1*p2));
  41. if (oper==4) text.setText(""+(p1/p2));
  42. }
  43.  
  44. else if (str=="+") {
  45. oper = 1;
  46. p1 = Double.parseDouble(text.getText());
  47. text.setText("");
  48. }
  49. else if(str=="-")
  50. {
  51.     oper = 2;
  52.     p1 = Double.parseDouble(text.getText());
  53.     text.setText("");
  54. }
  55.  
  56. else if(str=="*")
  57. {
  58.     oper = 3;
  59.     p1 = Double.parseDouble(text.getText());
  60.     text.setText("");
  61. }
  62.  
  63. else if(str=="/")
  64. {
  65.     oper = 4;
  66.     p1 = Double.parseDouble(text.getText());
  67.     text.setText("");
  68. }
  69. else
  70. {
  71.     text.setText(""+text.getText()+str);
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.     }
  83.  
  84.     public okno() {
  85.  
  86.     Container cont = getContentPane();
  87.     JPanel pan = new JPanel();
  88.  
  89.     pan.setLayout(null);
  90.  
  91.     Font btnFont = new Font("serif", 0, 20);
  92.  
  93.     Font labFont = new Font("arial", 1, 30);
  94.  
  95.     Font textFont = new Font("arial", 2, 30);
  96.  
  97.  
  98.     JButton[] btn1 = new JButton[4];
  99.     for (int i = 0; i < 4; i++) {
  100.     btn1[i] = new JButton();
  101.  
  102.     btn1[i].setSize(100, 40);
  103.  
  104.     btn1[i].setFont(btnFont);
  105.  
  106.     btn1[i].setLocation(150, 50 + i * 50);
  107.     btn1[i].addActionListener(new ActionListener() {
  108.     public void actionPerformed(ActionEvent e) {
  109.     btnClick((JButton) e.getSource());
  110.  
  111.     }
  112.  
  113.     });
  114.     pan.add(btn1[i]);
  115.  
  116.     }
  117.  
  118.     JButton[] btn2 = new JButton[4];
  119.     for (int i = 0; i < 4; i++) {
  120.     btn2[i] = new JButton();
  121.  
  122.     btn2[i].setSize(100, 40);
  123.  
  124.     btn2[i].setFont(btnFont);
  125.  
  126.     btn2[i].setLocation(300, 50 + i * 50);
  127.     btn2[i].addActionListener(new ActionListener() {
  128.     public void actionPerformed(ActionEvent e) {
  129.     btnClick((JButton) e.getSource());
  130.  
  131.     }
  132.  
  133.     });
  134.     pan.add(btn2[i]);
  135.  
  136.     }
  137.  
  138.     JButton[] btn3 = new JButton[4];
  139.     for (int i = 0; i < 4; i++) {
  140.     btn3[i] = new JButton();
  141.  
  142.     btn3[i].setSize(100, 40);
  143.  
  144.     btn3[i].setFont(btnFont);
  145.  
  146.     btn3[i].setLocation(450, 50 + i * 50);
  147.     btn3[i].addActionListener(new ActionListener() {
  148.     public void actionPerformed(ActionEvent e) {
  149.     btnClick((JButton) e.getSource());
  150.  
  151.     }
  152.  
  153.     });
  154.     pan.add(btn3[i]);
  155.  
  156.     }
  157.    
  158.     JButton[] btn4 = new JButton[4];
  159.     for (int i = 0; i < 4; i++) {
  160.     btn4[i] = new JButton();
  161.  
  162.     btn4[i].setSize(100, 40);
  163.  
  164.     btn4[i].setFont(btnFont);
  165.  
  166.     btn4[i].setLocation(600, 50 + i * 50);
  167.     btn4[i].addActionListener(new ActionListener() {
  168.     public void actionPerformed(ActionEvent e) {
  169.     btnClick((JButton) e.getSource());
  170.  
  171.     }
  172.  
  173.     });
  174.     pan.add(btn4[i]);
  175.  
  176.     }
  177.     JButton btn5 = new JButton();
  178.    
  179.     btn5 = new JButton();
  180.  
  181.     btn5.setSize(400, 30);
  182.  
  183.     btn5.setFont(btnFont);
  184.  
  185.     btn5.setLocation(225,250);
  186.     btn5.addActionListener(new ActionListener() {
  187.     public void actionPerformed(ActionEvent e) {
  188.     btnClick((JButton) e.getSource());
  189.  
  190.     }
  191.  
  192.     });
  193.     pan.add(btn5);
  194.  
  195.    
  196.    
  197.  
  198.    
  199.  
  200.     btn1[0].setText("1");
  201.  
  202.     btn1[1].setText("4");
  203.  
  204.     btn1[2].setText("7");
  205.  
  206.     btn1[3].setText("=");
  207.  
  208.     btn2[0].setText("2");
  209.  
  210.     btn2[1].setText("5");
  211.    
  212.     btn2[2].setText("8");
  213.    
  214.     btn2[3].setText("0");
  215.    
  216.     btn3[0].setText("3");
  217.    
  218.     btn3[1].setText("6");
  219.    
  220.     btn3[2].setText("9");
  221.    
  222.     btn3[3].setText("C");
  223.    
  224.     btn4[0].setText("+");
  225.    
  226.     btn4[1].setText("-");
  227.    
  228.     btn4[2].setText("*");
  229.  
  230.     btn4[3].setText("/");
  231.    
  232.     btn5.setText("Выход");
  233.    
  234.    
  235.    
  236.    
  237.     JLabel lab = new JLabel("Результат: ");
  238.     lab.setFont(labFont);
  239.     lab.setBounds(130, 0, 300, 50);
  240.     pan.add(lab);
  241.     text = new JTextArea();
  242.  
  243.     text.setFont(textFont);
  244.  
  245.     text.setBounds(300, 10, 300, 35);
  246.  
  247.     text.setForeground(new Color(0, 0, 100));
  248.  
  249.     text.setBackground(Color.WHITE);
  250.  
  251.     pan.add(text);
  252.  
  253.     cont.add(pan);
  254.  
  255.     setBounds(0, 0, 800, 600);
  256.  
  257.  
  258.     setTitle("Калькулятор");
  259.  
  260.  
  261.     setResizable(false);
  262.  
  263.     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  264.  
  265.  
  266.     setVisible(true);
  267.  
  268.     }
  269.  
  270.     }
Advertisement
Add Comment
Please, Sign In to add comment