import javax.swing.*; import java.awt.*; import java.awt.event.*; public class calculator9 { public static void main (String[] args) { CalcImage Calculator = new CalcImage(); Calculator.setTitle("Calculator"); Calculator.show(); } } class CalcImage extends JFrame { public CalcImage() { final int Window_Width = 250; final int Window_Height = 300; setSize(Window_Width, Window_Height); // add so that the window closes upon exit addWindowListener(new WindowCloser()); // now define the button elements BOne = new JButton("1"); BTwo = new JButton("2"); BThree = new JButton("3"); Bfour = new JButton("4"); Bfive = new JButton("5"); Bsix = new JButton("6"); Bseven = new JButton("7"); Beight = new JButton("8"); Bnine = new JButton("9"); Bzero = new JButton("0"); //coloring the number buttons BOne.setBackground(Color.orange); BOne.setForeground(Color.blue); BTwo.setBackground(Color.orange); BTwo.setForeground(Color.blue); BThree.setBackground(Color.orange); BThree.setForeground(Color.blue); Bfour.setBackground(Color.orange); Bfour.setForeground(Color.blue); Bfive.setBackground(Color.orange); Bfive.setForeground(Color.blue); Bsix.setBackground(Color.orange); Bsix.setForeground(Color.blue); Bseven.setBackground(Color.orange); Bseven.setForeground(Color.blue); Beight.setBackground(Color.orange); Beight.setForeground(Color.blue); Bnine.setBackground(Color.orange); Bnine.setForeground(Color.blue); Bzero.setBackground(Color.orange); Bzero.setForeground(Color.blue); BPlus = new JButton("+"); BMinus = new JButton("-"); BDiv = new JButton("/"); BMult = new JButton("x"); BEquals = new JButton("="); Bclear = new JButton("c"); // colour rest of the buttons BPlus.setBackground(Color.yellow); BPlus.setForeground(Color.blue); BMinus.setBackground(Color.yellow); BMinus.setForeground(Color.blue); BDiv.setBackground(Color.yellow); BDiv.setForeground(Color.blue); BMult.setBackground(Color.yellow); BMult.setForeground(Color.blue); BEquals.setBackground(Color.yellow); BEquals.setForeground(Color.blue); Bclear.setBackground(Color.yellow); Bclear.setForeground(Color.blue); // define the listeners ActionListener NListener = new NumberListener(); BOne.addActionListener(NListener); BTwo.addActionListener(NListener); BThree.addActionListener(NListener); Bfour.addActionListener(NListener); Bfive.addActionListener(NListener); Bsix.addActionListener(NListener); Bseven.addActionListener(NListener); Beight.addActionListener(NListener); Bnine.addActionListener(NListener); Bzero.addActionListener(NListener); ActionListener OpListener = new OperatorListener(); BPlus.addActionListener(OpListener); BMinus.addActionListener(OpListener); BMult.addActionListener(OpListener); BDiv.addActionListener(OpListener); Bclear.addActionListener(OpListener); ActionListener EqListener = new EqualsListener(); BEquals.addActionListener(EqListener); // Define 9-character display area of calculator JTextField DisplayArea = new JTextField(9); DisplayArea.setEditable(false); DisplayArea.setBackground(Color.blue); JTextField DisplayArea2 = new JTextField(9); DisplayArea2.setEditable(false); //DisplayArea.setBackground(Color.blue); // Define panel for all components JPanel SuperPanel = new JPanel(); //Define the container to hold everything Container ScreenPane = getContentPane(); //Add components to panel //DisplayPanel2.setLayout( new GridLayout(4,4)); DisplayArea.setLayout(new GridLayout(4,4,10,10)); DisplayArea.add(BOne); DisplayArea.add(BTwo); DisplayArea.add(BThree); DisplayArea.add(BPlus); DisplayArea.add(Bfour); DisplayArea.add(Bfive); DisplayArea.add(Bsix); DisplayArea.add(BMinus); DisplayArea.add(Bseven); DisplayArea.add(Beight); DisplayArea.add(Bnine); DisplayArea.add(BMult); DisplayArea.add(Bclear); DisplayArea.add(Bzero); DisplayArea.add(BEquals); DisplayArea.add(BDiv); DisplayArea2.setLayout(new GridLayout(1,1,1,1)); DisplayField = new JTextField(9); DisplayField.setEditable(false); DisplayField.setBackground(Color.white); DisplayArea2.add(DisplayField); //ScreenPane.add(SuperPanel, "Center"); ScreenPane.add(DisplayArea, "Center"); ScreenPane.add(DisplayArea2, "South"); DisplayArea2.setText(display); } // the action starts here private String display = ""; private String opcode = ""; private JTextField DisplayField; private double NumericValue = 0; private double Total = 0; private class NumberListener implements ActionListener // if a number button is clicked, add it to the string of #s { public void actionPerformed(ActionEvent event) { Object source = event.getSource(); if (source == BOne) display = display + "1"; else if (source == BTwo) display = display + "2"; else if (source == BThree) display = display + "3"; else if (source == Bfour) display = display + "4"; else if (source == Bfive) display = display + "5"; else if (source == Bsix) display = display + "6"; else if (source == Bseven) display = display + "7"; else if (source == Beight) display = display + "8"; else if (source == Bnine) display = display + "9"; else if (source == Bzero) display = display + "0"; DisplayField.setText(display); } } private class OperatorListener implements ActionListener // if an operator is clicked, save the number, // and keep track of which operation will happen // after the second number is entered { public void actionPerformed(ActionEvent event) { NumericValue = Double.parseDouble(display); Total = NumericValue; display = ""; Object source = event.getSource(); if (source == BPlus) opcode = "+"; else if (source == BMinus) opcode = "-"; else if (source == BMult) opcode = "*"; else if (source == BDiv) opcode = "/"; else if (source == Bclear) { opcode = ""; Total = 0; display = ""; DisplayField.setText(display); } } } private class EqualsListener implements ActionListener // if the equals button is clicked, get the number // from the string and then update the Total // using the previously saved operation code { public void actionPerformed(ActionEvent event) { NumericValue = Double.parseDouble(display); if (opcode == "+") Total = Total + NumericValue; else if (opcode == "-") Total = Total - NumericValue; else if (opcode == "*") Total = Total * NumericValue; else if (opcode == "/") Total = Total / NumericValue; display = String.valueOf(Total); DisplayField.setText(display); } } private JButton BOne, BTwo, BThree, Bfour, Bfive, Bsix, Bseven, Beight, Bnine, Bzero, BPlus, BMinus, BDiv, BMult, BEquals, Bdot, Bclear; private JTextField DisplayArea, DisplayArea2; private class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent event) { System.exit(0); } } }