Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package j19;
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- public class j18 {
- public static void main(String[] args) {
- okno nf = new okno();
- }
- }
- class okno extends JFrame {
- private JTextArea text;
- private double p1 = 0, p2 = 0;
- private int oper = 0;
- private void btnClick(JButton btn) {
- String str = btn.getText();
- if (str=="Выход") System.exit(0);
- else if (str=="C")
- {
- text.setText(" ");
- p1=p2=0;
- }
- else if (str=="=")
- {
- p2 = Integer.parseInt(text.getText());
- if (oper==1) text.setText(""+(p1+p2));
- if (oper==2) text.setText(""+(p1-p2));
- if (oper==3) text.setText(""+(p1*p2));
- if (oper==4) text.setText(""+(p1/p2));
- }
- else if (str=="+") {
- oper = 1;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else if(str=="-")
- {
- oper = 2;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else if(str=="*")
- {
- oper = 3;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else if(str=="/")
- {
- oper = 4;
- p1 = Double.parseDouble(text.getText());
- text.setText("");
- }
- else
- {
- text.setText(""+text.getText()+str);
- }
- }
- public okno() {
- Container cont = getContentPane();
- JPanel pan = new JPanel();
- pan.setLayout(null);
- Font btnFont = new Font("serif", 0, 20);
- Font labFont = new Font("arial", 1, 30);
- Font textFont = new Font("arial", 2, 30);
- JButton[] btn1 = new JButton[4];
- for (int i = 0; i < 4; i++) {
- btn1[i] = new JButton();
- btn1[i].setSize(100, 40);
- btn1[i].setFont(btnFont);
- btn1[i].setLocation(150, 50 + i * 50);
- btn1[i].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btnClick((JButton) e.getSource());
- }
- });
- pan.add(btn1[i]);
- }
- JButton[] btn2 = new JButton[4];
- for (int i = 0; i < 4; i++) {
- btn2[i] = new JButton();
- btn2[i].setSize(100, 40);
- btn2[i].setFont(btnFont);
- btn2[i].setLocation(300, 50 + i * 50);
- btn2[i].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btnClick((JButton) e.getSource());
- }
- });
- pan.add(btn2[i]);
- }
- JButton[] btn3 = new JButton[4];
- for (int i = 0; i < 4; i++) {
- btn3[i] = new JButton();
- btn3[i].setSize(100, 40);
- btn3[i].setFont(btnFont);
- btn3[i].setLocation(450, 50 + i * 50);
- btn3[i].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btnClick((JButton) e.getSource());
- }
- });
- pan.add(btn3[i]);
- }
- JButton[] btn4 = new JButton[4];
- for (int i = 0; i < 4; i++) {
- btn4[i] = new JButton();
- btn4[i].setSize(100, 40);
- btn4[i].setFont(btnFont);
- btn4[i].setLocation(600, 50 + i * 50);
- btn4[i].addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btnClick((JButton) e.getSource());
- }
- });
- pan.add(btn4[i]);
- }
- JButton btn5 = new JButton();
- btn5 = new JButton();
- btn5.setSize(400, 30);
- btn5.setFont(btnFont);
- btn5.setLocation(225,250);
- btn5.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- btnClick((JButton) e.getSource());
- }
- });
- pan.add(btn5);
- btn1[0].setText("1");
- btn1[1].setText("4");
- btn1[2].setText("7");
- btn1[3].setText("=");
- btn2[0].setText("2");
- btn2[1].setText("5");
- btn2[2].setText("8");
- btn2[3].setText("0");
- btn3[0].setText("3");
- btn3[1].setText("6");
- btn3[2].setText("9");
- btn3[3].setText("C");
- btn4[0].setText("+");
- btn4[1].setText("-");
- btn4[2].setText("*");
- btn4[3].setText("/");
- btn5.setText("Выход");
- JLabel lab = new JLabel("Результат: ");
- lab.setFont(labFont);
- lab.setBounds(130, 0, 300, 50);
- pan.add(lab);
- text = new JTextArea();
- text.setFont(textFont);
- text.setBounds(300, 10, 300, 35);
- text.setForeground(new Color(0, 0, 100));
- text.setBackground(Color.WHITE);
- pan.add(text);
- cont.add(pan);
- setBounds(0, 0, 800, 600);
- setTitle("Калькулятор");
- setResizable(false);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment