Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JTextField;
- public class Calculator
- extends JFrame
- implements ActionListener
- {
- private JLabel label1;
- private JLabel label2;
- private JLabel label3;
- private JLabel label4;
- private JTextField textfield1;
- private JTextField textfield2;
- private JTextField textfield3;
- private JButton boton1;
- private JButton boton2;
- private JButton boton3;
- private JButton boton4;
- private JButton boton5;
- private JButton boton6;
- public Calculator()
- {
- setLayout(null);
- this.label1 = new JLabel("Calculatorul lui Man :)");
- this.label1.setBounds(80, 20, 200, 20);
- add(this.label1);
- this.label2 = new JLabel("X=");
- this.label2.setBounds(10, 60, 50, 20);
- add(this.label2);
- this.label3 = new JLabel("Y=");
- this.label3.setBounds(150, 60, 50, 20);
- add(this.label3);
- this.label4 = new JLabel("Rezultat ==> ");
- this.label4.setBounds(10, 150, 80, 20);
- add(this.label4);
- this.textfield3 = new JTextField();
- this.textfield3.setBounds(120, 150, 100, 30);
- this.textfield3.setBackground(Color.lightGray);
- add(this.textfield3);
- this.textfield1 = new JTextField();
- this.textfield1.setBounds(30, 60, 100, 30);
- add(this.textfield1);
- this.textfield2 = new JTextField();
- this.textfield2.setBounds(170, 60, 100, 30);
- add(this.textfield2);
- this.boton1 = new JButton("+");
- this.boton1.setBounds(30, 200, 50, 30);
- add(this.boton1);
- this.boton1.addActionListener(this);
- this.boton2 = new JButton("-");
- this.boton2.setBounds(90, 200, 50, 30);
- add(this.boton2);
- this.boton2.addActionListener(this);
- this.boton3 = new JButton("*");
- this.boton3.setBounds(150, 200, 50, 30);
- add(this.boton3);
- this.boton3.addActionListener(this);
- this.boton4 = new JButton("/");
- this.boton4.setBounds(210, 200, 50, 30);
- add(this.boton4);
- this.boton4.addActionListener(this);
- this.boton5 = new JButton("Repeta");
- this.boton5.setBounds(30, 250, 100, 30);
- add(this.boton5);
- this.boton5.addActionListener(this);
- this.boton6 = new JButton("Iesire");
- this.boton6.setBounds(160, 250, 100, 30);
- add(this.boton6);
- this.boton6.addActionListener(this);
- setTitle("Calculator 4 operatii(+ - * /)");
- }
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource() == this.boton6) {
- System.exit(0);
- }
- double rezultat = 0.0D;
- String cad1 = this.textfield1.getText();
- String cad2 = this.textfield2.getText();
- double x = Double.parseDouble(cad1);
- double y = Double.parseDouble(cad2);
- if (e.getSource() == this.boton1)
- {
- rezultat = x + y;
- this.textfield3.setText(String.valueOf(rezultat));
- }
- else if (e.getSource() == this.boton2)
- {
- rezultat = x - y;
- this.textfield3.setText(String.valueOf(rezultat));
- }
- else if (e.getSource() == this.boton3)
- {
- rezultat = x * y;
- this.textfield3.setText(String.valueOf(rezultat));
- }
- else if (e.getSource() == this.boton4)
- {
- if (y == 0.0D)
- {
- setTitle("===>> Erroare !");
- }
- else
- {
- rezultat = x / y;
- this.textfield3.setText(String.valueOf(rezultat));
- }
- }
- else if (e.getSource() == this.boton5)
- {
- this.textfield1.setText(" ");
- this.textfield2.setText(" ");
- this.textfield3.setText(" ");
- }
- }
- public static void main(String[] ar)
- {
- Calculator formulario1 = new Calculator();
- formulario1.setBounds(0, 0, 300, 350);
- formulario1.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement