Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.GridLayout;
- import java.awt.Image;
- import java.awt.Toolkit;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JButton;
- import javax.swing.JPanel;
- import javax.swing.JTextField;
- import javax.swing.event.ChangeEvent;
- import javax.swing.event.ChangeListener;
- class Cal extends javax.swing.JFrame implements ActionListener {
- private DigitButton plusminus, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, bpoint;
- private OpButton add, sub, mul, div, del, clear, inv, sqrt, equal;
- JTextField tf;
- static double a = 0, b = 0, result = 0;
- static int operator = 0;
- private int point;
- private Image icon;
- Cal() {
- super("Calculator");
- point = 0;
- icon = Toolkit.getDefaultToolkit().getImage("icon.png"); // Loading icon image into memory
- /************* Initialising buttons ************/
- b1 = new DigitButton("1");
- b2 = new DigitButton("2");
- b3 = new DigitButton("3");
- b4 = new DigitButton("4");
- b5 = new DigitButton("5");
- b6 = new DigitButton("6");
- b7 = new DigitButton("7");
- b8 = new DigitButton("8");
- b9 = new DigitButton("9");
- b0 = new DigitButton("0");
- bpoint = new DigitButton(".");
- inv = new OpButton("1/x");
- sqrt = new OpButton("√");
- plusminus = new DigitButton("±");
- del = new OpButton("DEL");
- add = new OpButton("+");
- sub = new OpButton("-");
- mul = new OpButton("×");
- div = new OpButton("÷");
- clear = new OpButton("C");
- equal = new OpButton("=");
- // TextField in which result is to be displayed
- tf = new JTextField();
- tf.setEditable(false);
- tf.setFont(new Font("Segoi UI", Font.BOLD, 25)); // Setting font
- // tf.setBackground(Color.DARK_GRAY.darker().darker());
- // tf.setForeground(Color.WHITE.brighter());
- tf.setBackground(Color.WHITE);
- tf.setForeground(Color.BLACK);
- tf.setText("0");
- tf.setBorder(null);
- /********** Setting the location of the buttons ***********/
- JPanel panel = new JPanel();
- JPanel panel2 = new JPanel();
- JPanel panel3 = new JPanel();
- JPanel panel4 = new JPanel();
- panel.add(b7);
- panel.add(b8);
- panel.add(b9);
- panel.add(b4);
- panel.add(b5);
- panel.add(b6);
- panel.add(b1);
- panel.add(b2);
- panel.add(b3);
- panel2.add(plusminus);
- panel2.add(b0);
- panel2.add(bpoint);
- panel3.add(div);
- panel3.add(mul);
- panel3.add(sub);
- panel3.add(add);
- panel3.add(equal);
- panel4.add(clear);
- panel4.add(sqrt);
- panel4.add(inv);
- panel4.add(del);
- GridLayout grid = new GridLayout(3, 3);
- GridLayout grid2 = new GridLayout(1, 2);
- GridLayout grid3 = new GridLayout(5, 1);
- GridLayout grid4 = new GridLayout(1, 4);
- panel.setLayout(grid);
- panel.setBounds(0, 160, 240, 240);
- panel2.setLayout(grid2);
- panel2.setBounds(0, 400, 240, 80);
- panel3.setLayout(grid3);
- panel3.setBounds(240, 160, 80, 320);
- panel4.setLayout(grid4);
- panel4.setBounds(0, 80, 320, 80);
- tf.setBounds(0, 0, 320, 80);
- tf.setHorizontalAlignment(JTextField.RIGHT); // Setting alignment
- /******** Adding Panels to the Frame ***********/
- add(panel);
- add(panel2);
- add(panel3);
- add(panel4);
- add(tf);
- /* Adding ActionListeners to the buttons */
- b0.addActionListener(this);
- b1.addActionListener(this);
- b2.addActionListener(this);
- b3.addActionListener(this);
- b4.addActionListener(this);
- b5.addActionListener(this);
- b6.addActionListener(this);
- b7.addActionListener(this);
- b8.addActionListener(this);
- b9.addActionListener(this);
- bpoint.addActionListener(this);
- clear.addActionListener(this);
- equal.addActionListener(this);
- add.addActionListener(this);
- sub.addActionListener(this);
- mul.addActionListener(this);
- div.addActionListener(this);
- plusminus.addActionListener(this);
- sqrt.addActionListener(this);
- inv.addActionListener(this);
- del.addActionListener(this);
- ////////////////////////////////////////////
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setResizable(false);
- setIconImage(icon);
- setLocationRelativeTo(null);
- setSize(335, 515);
- setLayout(null);
- setVisible(true);
- }
- private boolean isZero(String Text) {
- if (Text.equals("0"))
- return true;
- else
- return false;
- }
- private void getInput(ActionEvent e) {
- if (tf.getText().equals("∞")) {
- } else if (tf.getText().equals("Invalid input")) {
- } else if (e.getSource().equals(b0)) {
- if (isZero(tf.getText())) {
- tf.setText("0");
- } else {
- tf.setText(tf.getText().concat("0"));
- }
- } else if (e.getSource().equals(b1)) {
- if (isZero(tf.getText())) {
- tf.setText("1");
- } else {
- tf.setText(tf.getText().concat("1"));
- }
- } else if (e.getSource().equals(b2)) {
- if (isZero(tf.getText())) {
- tf.setText("2");
- } else {
- tf.setText(tf.getText().concat("2"));
- }
- } else if (e.getSource().equals(b3)) {
- if (isZero(tf.getText())) {
- tf.setText("3");
- } else {
- tf.setText(tf.getText().concat("3"));
- }
- } else if (e.getSource().equals(b4)) {
- if (isZero(tf.getText())) {
- tf.setText("4");
- } else {
- tf.setText(tf.getText().concat("4"));
- }
- } else if (e.getSource().equals(b5)) {
- if (isZero(tf.getText())) {
- tf.setText("5");
- } else {
- tf.setText(tf.getText().concat("5"));
- }
- } else if (e.getSource().equals(b6)) {
- if (isZero(tf.getText())) {
- tf.setText("6");
- } else {
- tf.setText(tf.getText().concat("6"));
- }
- } else if (e.getSource().equals(b7)) {
- if (isZero(tf.getText())) {
- tf.setText("7");
- } else {
- tf.setText(tf.getText().concat("7"));
- }
- } else if (e.getSource().equals(b8)) {
- if (isZero(tf.getText())) {
- tf.setText("8");
- } else {
- tf.setText(tf.getText().concat("8"));
- }
- } else if (e.getSource().equals(b9)) {
- if (isZero(tf.getText())) {
- tf.setText("9");
- } else {
- tf.setText(tf.getText().concat("9"));
- }
- } else if (e.getSource().equals(bpoint)) {
- if (point < 1) {
- tf.setText(tf.getText().concat("."));
- point = 1;
- } else {
- }
- } else if (e.getSource().equals(sqrt)) {
- double val = Double.parseDouble(tf.getText());
- if (val < 0) {
- tf.setText("Invalid input");
- } else {
- point = 1;
- val = Math.sqrt(val);
- tf.setText(String.valueOf(val));
- }
- } else if (e.getSource().equals(inv)) {
- double val = Double.valueOf(tf.getText());
- if (val == 0) {
- tf.setText("∞");
- } else {
- point = 1;
- val = 1 / val;
- tf.setText(String.valueOf(val));
- }
- } else if (e.getSource().equals(plusminus)) {
- double val = Double.valueOf(tf.getText());
- if (val == 0) {
- tf.setText("0");
- } else {
- point = 1;
- val = -val;
- tf.setText(String.valueOf(val));
- }
- }
- }
- public void actionPerformed(ActionEvent e) {
- if (tf.getText().equals("+") || tf.getText().equals("-") || tf.getText().equals("×")
- || tf.getText().equals("÷")) {
- tf.setText("");
- getInput(e);
- } else if (e.getSource().equals(clear)) {
- point = 0;
- tf.setText("0");
- } else if (e.getSource().equals(del)) {
- String s = tf.getText();
- tf.setText("");
- if (s.equals("∞")) {
- tf.setText("0");
- point = 0;
- } else if (s.equals("Invalid input")) {
- tf.setText("0");
- point = 0;
- } else if (Double.valueOf(s) < 0) {
- if (s.length() < 3) {
- if (s.matches("-")) {
- point = 0;
- tf.setText("0");
- } else {
- point = 0;
- tf.setText("0");
- }
- } else {
- for (int i = 0; i < s.length() - 1; i++) {
- tf.setText(tf.getText() + s.charAt(i));
- }
- if (tf.getText().contains(".")) {
- point = 1;
- } else {
- point = 0;
- }
- }
- } else {
- if (s.length() < 2) {
- /*
- * if (s.matches("-")) { Text = "0"; tf.setText(Text); } else {
- */
- point = 0;
- tf.setText("0");
- // }
- } else {
- for (int i = 0; i < s.length() - 1; i++) {
- tf.setText(tf.getText() + s.charAt(i));
- }
- if (tf.getText().contains(".")) {
- point = 1;
- } else {
- point = 0;
- }
- }
- }
- } else if (tf.getText().equals("∞")) {
- } else if (tf.getText().equals("Invalid input")) {
- } else if (e.getSource().equals(add)) {
- a = Double.valueOf(tf.getText());
- operator = 1;
- tf.setText("+");
- } else if (e.getSource().equals(sub)) {
- a = Double.valueOf(tf.getText());
- operator = 2;
- tf.setText("-");
- } else if (e.getSource().equals(mul)) {
- a = Double.valueOf(tf.getText());
- operator = 3;
- tf.setText("×");
- } else if (e.getSource().equals(div)) {
- a = Double.valueOf(tf.getText());
- operator = 4;
- tf.setText("÷");
- } else if (e.getSource().equals(equal)) {
- point = 1;
- tf.setText(calcInput(operator, a));
- } else {
- getInput(e);
- }
- }
- private String calcInput(int operator, double a) {
- b = Double.valueOf(tf.getText());
- String Result;
- switch (operator) {
- case 1:
- result = a + b;
- break;
- case 2:
- result = a - b;
- break;
- case 3:
- result = a * b;
- break;
- case 4:
- if (b != 0)
- result = a / b;
- else {
- result = 0;
- tf.setText("∞");
- }
- }
- if (b == 0)
- Result = "∞";
- else
- Result = String.valueOf(result);
- operator = 0;
- return Result;
- }
- }
- public class Calc {
- public static void main(String[] args) {
- new Cal();
- }
- }
- class OpButton extends JButton {
- private Color bgcolor, frcolor;
- OpButton(String op) {
- super(op);
- setBorderPainted(false);
- setFocusPainted(false);
- setFont(new Font("Segoi UI", Font.PLAIN, 22));
- bgcolor = new Color(0, 153, 255);
- frcolor = Color.WHITE;
- setContentAreaFilled(false);
- setOpaque(true);
- setBackground(bgcolor);
- setForeground(frcolor);
- setText(op);
- addChangeListener(new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent evt) {
- if (getModel().isPressed()) {
- setBackground(bgcolor.darker().darker());
- } else if (getModel().isRollover()) {
- setBackground(bgcolor.darker());
- } else {
- setBackground(bgcolor);
- }
- }
- });
- }
- }
- class DigitButton extends JButton {
- private Color bgcolor, color2;
- final static long serialVersionUID = 2;
- DigitButton(String digit) {
- super(digit);
- setBorderPainted(false);
- setFocusPainted(false);
- bgcolor = Color.WHITE;
- color2 = new Color(0, 153, 255);
- setFont(new Font("Segoi UI", Font.BOLD, 22));
- setContentAreaFilled(false);
- setOpaque(true);
- // setForeground(Color.BLACK.darker().darker());
- setText(digit);
- setBackground(bgcolor);
- setForeground(color2);
- addChangeListener(new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent evt) {
- /*
- * if (getModel().isPressed()) { setBackground(bgcolor.darker().darker()); //
- * setForeground(Color.WHITE); } else if (getModel().isRollover()) {
- * setBackground(bgcolor.darker().darker()); // setForeground(Color.WHITE); }
- * else { // setForeground(color2); setBackground(bgcolor.darker()); }
- */
- if (getModel().isPressed()) {
- setBackground(bgcolor.darker().darker());
- setForeground(Color.WHITE);
- } else if (getModel().isRollover()) {
- setBackground(bgcolor.darker());
- setForeground(Color.WHITE);
- } else {
- setBackground(bgcolor);
- setForeground(color2);
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement