Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- public class Frame extends JFrame implements ActionListener
- {
- JLayeredPane layeredPane;
- JButton button1;
- JButton button2;
- JButton button3;
- JButton button4;
- JButton button5;
- JButton button6;
- JButton button7;
- JButton button8;
- JButton button9;
- String result = "0";
- JLabel numberGrid;
- JLabel resultLabel;
- JLabel operationsLabel;
- JButton plus;
- JButton minus;
- JButton times;
- JButton division;
- JButton equals;
- JButton log;
- JButton ac;
- JLabel equalsLabel;
- Font font;
- int timesClicked = 0;
- double a;
- double b;
- double answer = 0;
- int operation;
- public Frame()
- {
- JFrame frame = new JFrame();
- frame.setSize(1000, 825);
- frame.setLocationRelativeTo(null);
- frame.setResizable(false);
- frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
- font = new Font("SANS_SERIF", Font.PLAIN, 24);
- layeredPane = frame.getLayeredPane();
- layeredPane.setVisible(true);
- layeredPane.setLayout(null);
- numberGrid = new JLabel();
- numberGrid.setBorder(BorderFactory.createLineBorder((Color.black), 12));
- numberGrid.setBounds(0,200,600,600);
- numberGrid.setLayout(new GridLayout(3,3));
- layeredPane.add(numberGrid, 0);
- resultLabel = new JLabel(result);
- resultLabel.setBorder(BorderFactory.createLineBorder((Color.white), 12));
- resultLabel.setBounds(0,0,1000,200);
- resultLabel.setOpaque(true);
- resultLabel.setFont(font);
- layeredPane.add(resultLabel);
- operationsLabel = new JLabel();
- operationsLabel.setBorder(BorderFactory.createLineBorder((Color.black), 12));
- operationsLabel.setBounds(600,200,400,400);
- operationsLabel.setBackground(Color.gray);
- operationsLabel.setOpaque(true);
- operationsLabel.setLayout(new GridLayout(2, 2));
- layeredPane.add(operationsLabel);
- equalsLabel = new JLabel();
- equalsLabel.setOpaque(true);
- equalsLabel.setBounds(600,600,400,200);
- equalsLabel.setLayout(new BorderLayout());
- equalsLabel.setBorder(BorderFactory.createLineBorder((Color.black), 12));
- layeredPane.add(equalsLabel);
- button1 = new JButton("1");
- button1.addActionListener(this);
- button1.setFont(font);
- numberGrid.add(button1);
- button2 = new JButton("2");
- button2.addActionListener(this);
- button2.setFont(font);
- numberGrid.add(button2);
- button3 = new JButton("3");
- button3.addActionListener(this);
- button3.setFont(font);
- numberGrid.add(button3);
- button4 = new JButton("4");
- button4.addActionListener(this);
- button4.setFont(font);
- numberGrid.add(button4);
- button5 = new JButton("5");
- button5.addActionListener(this);
- button5.setFont(font);
- numberGrid.add(button5);
- button6 = new JButton("6");
- button6.addActionListener(this);
- button6.setFont(font);
- numberGrid.add(button6);
- button7 = new JButton("7");
- button7.addActionListener(this);
- button7.setFont(font);
- numberGrid.add(button7);
- button8 = new JButton("8");
- button8.addActionListener(this);
- button8.setFont(font);
- numberGrid.add(button8);
- button9 = new JButton("9");
- button9.addActionListener(this);
- button9.setFont(font);
- numberGrid.add(button9);
- plus = new JButton("+");
- plus.addActionListener(this);
- plus.setFont(font);
- operationsLabel.add(plus);
- minus = new JButton("-");
- minus.addActionListener(this);
- minus.setFont(font);
- operationsLabel.add(minus);
- times = new JButton("X");
- times.addActionListener(this);
- times.setFont(font);
- operationsLabel.add(times);
- division = new JButton("/");
- division.addActionListener(this);
- division.setFont(font);
- operationsLabel.add(division);
- log = new JButton("log(a|b)");
- log.addActionListener(this);
- log.setFont(font);
- operationsLabel.add(log);
- ac = new JButton("AC");
- ac.addActionListener(this);
- ac.setFont(font);
- operationsLabel.add(ac);
- equals = new JButton("=");
- equals.addActionListener(this);
- equals.setFont(font);
- equalsLabel.add(equals);
- frame.setVisible(true);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource() instanceof JButton)
- {
- JButton tempButton = (JButton)e.getSource();
- if(!(tempButton == plus || tempButton == minus || tempButton == division || tempButton == times || tempButton == equals || tempButton == log || tempButton == ac))
- {
- resultLabel.setText(tempButton.getText());
- if(timesClicked == 0)
- {
- a = (double)Integer.parseInt(tempButton.getText());
- timesClicked++;
- }
- else if(timesClicked >= 1)
- {
- b = (double)Integer.parseInt(tempButton.getText());
- timesClicked++;
- }
- }
- if(tempButton == plus || tempButton == minus || tempButton == times || tempButton == division || tempButton == log || tempButton == ac)
- {
- switch(tempButton.getText())
- {
- case "+": operation = 1;
- break;
- case "-": operation = 2;
- break;
- case "X": operation = 3;
- break;
- case "/": operation = 4;
- break;
- case "log(a|b)": operation = 5;
- break;
- default: break;
- }
- }
- if(tempButton == equals)
- {
- switch(operation)
- {
- case 1: answer = a + b;
- break;
- case 2: answer = a - b;
- break;
- case 3: answer = a*b;
- break;
- case 4: answer = a/b;
- break;
- case 5: answer = (Math.log(a))/(Math.log(b));
- break;
- }
- resultLabel.setText("" + answer);
- a = answer;
- b = 0;
- }
- if(tempButton == ac)
- {
- timesClicked = 0;
- answer = 0;
- a = 0;
- resultLabel.setText("Reset");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment