Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.EventQueue;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.border.EmptyBorder;
- import javax.swing.JTextField;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.awt.Font;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- public class Calculator extends JFrame {
- private static final long serialVersionUID = 1L;
- public static float a = 0;
- public static float b = 0;
- protected static int op = 0;
- private JPanel contentPane;
- private JTextField textField;
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Calculator frame = new Calculator();
- frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public Calculator() {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setBounds(100, 100, 450, 300);
- contentPane = new JPanel();
- contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
- setContentPane(contentPane);
- contentPane.setLayout(null);
- textField = new JTextField();
- textField.setEditable(false);
- textField.setBounds(10, 11, 198, 20);
- contentPane.add(textField);
- textField.setColumns(10);
- JButton btnNewButton = new JButton("C");
- btnNewButton.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText("");
- }
- });
- btnNewButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- btnNewButton.setBounds(302, 10, 48, 23);
- contentPane.add(btnNewButton);
- JButton btnAc = new JButton("AC");
- btnAc.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Calculator.a = (float) 0.0;
- Calculator.b = (float) 0.0;
- Calculator.b = 0;
- textField.setText("");
- }
- });
- btnAc.setBounds(360, 10, 64, 23);
- contentPane.add(btnAc);
- JButton btnNewButton_1 = new JButton("7");
- btnNewButton_1.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "7");
- }
- });
- btnNewButton_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
- btnNewButton_1.setBounds(10, 42, 48, 48);
- contentPane.add(btnNewButton_1);
- JButton button = new JButton("8");
- button.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "8");
- }
- });
- button.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button.setBounds(76, 42, 48, 48);
- contentPane.add(button);
- JButton button_1 = new JButton("9");
- button_1.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "9");
- }
- });
- button_1.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_1.setBounds(150, 42, 48, 48);
- contentPane.add(button_1);
- JButton button_2 = new JButton("6");
- button_2.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "6");
- }
- });
- button_2.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_2.setBounds(150, 101, 48, 48);
- contentPane.add(button_2);
- JButton button_3 = new JButton("5");
- button_3.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "5");
- }
- });
- button_3.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_3.setBounds(76, 101, 48, 48);
- contentPane.add(button_3);
- JButton button_4 = new JButton("4");
- button_4.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "4");
- }
- });
- button_4.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_4.setBounds(10, 101, 48, 48);
- contentPane.add(button_4);
- JButton button_5 = new JButton("3");
- button_5.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "3");
- }
- });
- button_5.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_5.setBounds(150, 160, 48, 48);
- contentPane.add(button_5);
- JButton button_6 = new JButton("2");
- button_6.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "2");
- }
- });
- button_6.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_6.setBounds(76, 160, 48, 48);
- contentPane.add(button_6);
- JButton button_7 = new JButton("1");
- button_7.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- textField.setText(textField.getText() + "1");
- }
- });
- button_7.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- }
- });
- button_7.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_7.setBounds(10, 160, 48, 48);
- contentPane.add(button_7);
- JButton button_8 = new JButton("\u221A");
- button_8.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Calculator.a = Float.parseFloat(textField.getText());
- Calculator.op = 6;
- textField.setText("");
- }
- });
- button_8.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_8.setBounds(333, 44, 66, 48);
- contentPane.add(button_8);
- JButton button_9 = new JButton("^");
- button_9.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Calculator.a = Float.parseFloat(textField.getText());
- Calculator.op = 5;
- textField.setText("");
- }
- });
- button_9.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_9.setBounds(257, 44, 66, 48);
- contentPane.add(button_9);
- JButton button_10 = new JButton("/");
- button_10.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Calculator.a = Float.parseFloat(textField.getText());
- Calculator.op = 4;
- textField.setText("");
- }
- });
- button_10.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_10.setBounds(333, 103, 66, 48);
- contentPane.add(button_10);
- JButton button_11 = new JButton("*");
- button_11.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Calculator.a = Float.parseFloat(textField.getText());
- Calculator.op = 3;
- textField.setText("");
- }
- });
- button_11.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_11.setBounds(257, 103, 66, 48);
- contentPane.add(button_11);
- JButton button_12 = new JButton("-");
- button_12.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Calculator.a = Float.parseFloat(textField.getText());
- Calculator.op = 2;
- textField.setText("");
- }
- });
- button_12.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_12.setBounds(333, 162, 66, 48);
- contentPane.add(button_12);
- JButton button_13 = new JButton("+");
- button_13.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- Calculator.a = Float.parseFloat(textField.getText());
- Calculator.op = 1;
- textField.setText("");
- }
- });
- button_13.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_13.setBounds(257, 162, 66, 48);
- contentPane.add(button_13);
- JButton btnNewButton_2 = new JButton("=");
- btnNewButton_2.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- if (Calculator.b == 0) {
- Calculator.b = Float.parseFloat(textField.getText());
- }
- if (Calculator.op == 1) {
- textField.setText(Float.toString(Calculator.a + Calculator.b));
- Calculator.a = Calculator.a + Calculator.b;
- }
- if (Calculator.op == 2) {
- textField.setText(Float.toString(Calculator.a - Calculator.b));
- Calculator.a = Calculator.a - Calculator.b;
- }
- if (Calculator.op == 3) {
- textField.setText(Float.toString(Calculator.a * Calculator.b));
- Calculator.a = Calculator.a * Calculator.b;
- }
- if (Calculator.op == 4) {
- textField.setText(Float.toString(Calculator.a / Calculator.b));
- Calculator.a = Calculator.a / Calculator.b;
- }
- if (Calculator.op == 5) {
- textField.setText(Float.toString((int) Math.pow(Calculator.a,
- Calculator.b)));
- Calculator.a = (int) Math.pow(Calculator.a, Calculator.b);
- }
- if (Calculator.op == 6) {
- float h = 1 / Calculator.b;
- textField.setText(Float.toString((float) Math.pow(Calculator.a,
- h)));
- Calculator.a = (float) Math.pow(Calculator.a, h);
- }
- }
- });
- btnNewButton_2.setBounds(257, 214, 142, 37);
- contentPane.add(btnNewButton_2);
- JButton btnDel = new JButton("DEL");
- btnDel.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- StringBuilder j = new StringBuilder();
- j.append(textField.getText());
- textField.setText(j.substring(0,
- textField.getText().length() - 1));
- }
- });
- btnDel.setBounds(218, 10, 74, 23);
- contentPane.add(btnDel);
- JButton button_14 = new JButton("0");
- button_14.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {textField.setText(textField.getText() + "0");
- }
- });
- button_14.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_14.setBounds(76, 214, 48, 48);
- contentPane.add(button_14);
- JButton button_15 = new JButton(".");
- button_15.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseClicked(MouseEvent e) {
- boolean g=true;
- String b=".";
- for(int a=0;a<textField.getText().length();a++){
- if(textField.getText().charAt(a)==b.charAt(0)){g=false;}
- }
- if(g){
- textField.setText(textField.getText() + ".");}
- }
- });
- button_15.setFont(new Font("Tahoma", Font.PLAIN, 20));
- button_15.setBounds(10, 214, 48, 48);
- contentPane.add(button_15);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement