import java.awt.*; import java.awt.FlowLayout; import java.awt.event.*; import java.awt.Color.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; public class Calculator extends JFrame implements ActionListener{ final int MAX_INPUT_LENGTH = 20; final int INPUT_MODE = 0; final int RESULT_MODE = 1; final int ERROR_MODE = 2; int displayMode; double lastNumber; boolean clearOnNextDigit, percent; String lastOperator; JButton but0, but1, but2, but3, but4, but5, but6, but7, but8, but9, but10, but11, but12, but13, but14, but15, but16, but17, but18, but19, but20, but21, but22; JMenu jmenuEdit, jmenuView, jmenuHelp; JMenuItem jmenuitemCopy, jmenuitemPaste, jmenuitemStandard, jmenuitemScientific, jmenuitemDigit, jmenuitemHelp, jmenuitemAbout; Border raisedbevel, loweredbevel, border; JLabel MemoryTextfield, LabelOutPut; public Calculator() { setBackground(new Color(236,233,216)); setIconImage(new ImageIcon("6039.png").getImage()); border = LineBorder.createGrayLineBorder(); raisedbevel = BorderFactory.createRaisedBevelBorder(); loweredbevel = BorderFactory.createLoweredBevelBorder(); Font f12 = new Font("MS Shell Dlg", 0, 11); Font f121 = new Font("MS Shell Dlg", 0, 12); /*Edit menu*/ jmenuEdit = new JMenu("Edit"); jmenuEdit.setFont(f121); jmenuEdit.setMnemonic(KeyEvent.VK_F); jmenuitemCopy = new JMenuItem("Copy"); jmenuitemCopy.setFont(f12); jmenuitemCopy.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_C, ActionEvent.CTRL_MASK)); jmenuitemPaste = new JMenuItem("Paste"); jmenuitemPaste.setFont(f12); jmenuitemPaste.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_V, ActionEvent.CTRL_MASK)); jmenuEdit.add(jmenuitemCopy); jmenuEdit.add(jmenuitemPaste); /*View menu*/ jmenuView = new JMenu("View"); jmenuView.setFont(f121); jmenuView.setMnemonic(KeyEvent.VK_H); jmenuitemStandard = new JMenuItem("Standard"); jmenuitemStandard.setFont(f12); jmenuitemScientific = new JMenuItem("Scientific"); jmenuitemScientific.setFont(f12); jmenuitemDigit = new JMenuItem("Digital grouping"); jmenuitemDigit.setFont(f12); jmenuView.add(jmenuitemStandard); jmenuView.add(jmenuitemScientific); jmenuView.add(jmenuitemDigit); /*Help menu*/ jmenuHelp = new JMenu("Help"); jmenuHelp.setFont(f121); jmenuHelp.setMnemonic(KeyEvent.VK_H); jmenuitemHelp = new JMenuItem("Help Topics"); jmenuitemHelp.setFont(f12); jmenuitemAbout = new JMenuItem("About calculator"); jmenuitemAbout.setFont(f12); jmenuHelp.add(jmenuitemHelp); jmenuHelp.add(jmenuitemAbout); JMenuBar mb = new JMenuBar(); mb.add(jmenuEdit); mb.add(jmenuView); mb.add(jmenuHelp); //setJMenuBar(mb); // Add components to frame add(mb,BorderLayout.NORTH); JPanel jplBackSpace, jplControl, jplButtons; jplButtons = new JPanel(); // Set jplButton panel layout manager for a 4 by 5 grid jplButtons.setLayout(new GridLayout(4, 5, 5, 5)); jplButtons.setBorder(new EmptyBorder(4, 12, 0, 0)); // Create numeric Jbuttons but1 = new JButton("1"); but1.setMargin(new Insets(0, 0, 0, 0)); but1.setFont(f12); but1.setForeground(Color.blue); but1.setBackground(new Color(242,243,238)); but1.addActionListener(this); but2 = new JButton("2"); but2.setMargin(new Insets(0, 0, 0, 0)); but2.setFont(f12); but2.setForeground(Color.blue); but2.setBackground(new Color(242,243,238)); but2.addActionListener(this); but3 = new JButton("3"); but3.setMargin(new Insets(0, 0, 0, 0)); but3.setFont(f12); but3.setForeground(Color.blue); but3.setBackground(new Color(242,243,238)); but3.addActionListener(this); but4 = new JButton("4"); but4.setMargin(new Insets(0, 0, 0, 0)); but4.setFont(f12); but4.setForeground(Color.blue); but4.setBackground(new Color(242,243,238)); but4.addActionListener(this); but5 = new JButton("5"); but5.setMargin(new Insets(0, 0, 0, 0)); but5.setFont(f12); but5.setForeground(Color.blue); but5.setBackground(new Color(242,243,238)); but5.addActionListener(this); but6 = new JButton("6"); but6.setMargin(new Insets(0, 0, 0, 0)); but6.setFont(f12); but6.setForeground(Color.blue); but6.setBackground(new Color(242,243,238)); but6.addActionListener(this); but7 = new JButton("7"); but7.setMargin(new Insets(0, 0, 0, 0)); but7.setFont(f12); but7.setForeground(Color.blue); but7.setBackground(new Color(242,243,238)); but7.addActionListener(this); but8 = new JButton("8"); but8.setMargin(new Insets(0, 0, 0, 0)); but8.setFont(f12); but8.setForeground(Color.blue); but8.setBackground(new Color(242,243,238)); but8.addActionListener(this); but9 = new JButton("9"); but9.setMargin(new Insets(0, 0, 0, 0)); but9.setFont(f12); but9.setForeground(Color.blue); but9.setBackground(new Color(242,243,238)); but9.addActionListener(this); but0 = new JButton("0"); but0.setMargin(new Insets(0, 0, 0, 0)); but0.setFont(f12); but0.setForeground(Color.blue); but0.setBackground(new Color(242,243,238)); but0.addActionListener(this); // Create operator Jbuttons but10 = new JButton("+/-"); but10.setMargin(new Insets(0, 0, 0, 0)); but10.setFont(f12); but10.setForeground(Color.blue); but10.setBackground(new Color(242,243,238)); but10.addActionListener(this); but11 = new JButton("."); but11.setMargin(new Insets(0, 0, 0, 0)); but11.setFont(f12); but11.setForeground(Color.blue); but11.setBackground(new Color(242,243,238)); but11.addActionListener(this); but12 = new JButton("="); but12.setMargin(new Insets(0, 0, 0, 0)); but12.setFont(f12); but12.setForeground(Color.red); but12.setBackground(new Color(242,243,238)); but12.addActionListener(this); but13 = new JButton("/"); but13.setMargin(new Insets(0, 0, 0, 0)); but13.setFont(f12); but13.setForeground(Color.red); but13.setBackground(new Color(242,243,238)); but13.addActionListener(this); but14 = new JButton("*"); but14.setMargin(new Insets(0, 0, 0, 0)); but14.setFont(f12); but14.setForeground(Color.red); but14.setBackground(new Color(242,243,238)); but14.addActionListener(this); but15 = new JButton("-"); but15.setFont(f12); but15.setForeground(Color.red); but15.setMargin(new Insets(0, 0, 0, 0)); but15.setBackground(new Color(242,243,238)); but15.addActionListener(this); but16 = new JButton("+"); but16.setFont(f12); but16.setForeground(Color.red); but16.setMargin(new Insets(0, 0, 0, 0)); but16.setBackground(new Color(242,243,238)); but16.addActionListener(this); but17 = new JButton("sqrt"); but17.setFont(f12); but17.setForeground(Color.blue); but17.setMargin(new Insets(0, 0, 0, 0)); but17.setBackground(new Color(242,243,238)); but17.addActionListener(this); but18 = new JButton("1/x"); but18.setFont(f12); but18.setForeground(Color.blue); but18.setMargin(new Insets(0, 0, 0, 0)); but18.setBackground(new Color(242,243,238)); but18.addActionListener(this); but19 = new JButton("%"); but19.setFont(f12); but19.setForeground(Color.blue); but19.setMargin(new Insets(0, 0, 0, 0)); but19.setBackground(new Color(242,243,238)); but19.addActionListener(this); //panel is the main Jpanel, so it contains jplControl, jplControl2, JButtons JPanel panel = new JPanel(new BorderLayout(0, 0)); getContentPane().add(panel, BorderLayout.CENTER); panel.setBorder(new EmptyBorder(3, 9, 10, 9)); // panel.setBackground(Color.yellow); //output numbers, result of calculations LabelOutPut = new JLabel("0"); LabelOutPut.setHorizontalAlignment(JLabel.RIGHT); LabelOutPut.setBackground(Color.WHITE); LabelOutPut.setFont(f121); LabelOutPut.setOpaque(true); LabelOutPut.setPreferredSize(new Dimension(200,23)); LabelOutPut.setBorder(loweredbevel); panel.add(LabelOutPut, BorderLayout.NORTH); /* JFormattedTextField formattedTextField = new JFormattedTextField(); panel.add(formattedTextField, BorderLayout.NORTH); formattedTextField.setHorizontalAlignment(JFormattedTextField.RIGHT); formattedTextField.setPreferredSize(new Dimension(105,22)); panel.add(formattedTextField,BorderLayout.NORTH); */ //text field for MC, MR, MS, M+ /* JFormattedTextField MemoryTextfield = new JFormattedTextField(); MemoryTextfield.setBackground(new Color(237,237,237)); MemoryTextfield.setPreferredSize(new Dimension(27,26)); */ MemoryTextfield = new JLabel(""); MemoryTextfield.setHorizontalAlignment(JLabel.RIGHT); MemoryTextfield.setBackground(new Color(237,237,237)); MemoryTextfield.setFont(f121); MemoryTextfield.setOpaque(true); MemoryTextfield.setPreferredSize(new Dimension(27,27)); MemoryTextfield.setBorder(loweredbevel); panel.add(LabelOutPut, BorderLayout.NORTH); JPanel panel_1 = new JPanel(); panel_1.setBorder(new EmptyBorder(0, 0, 0, 0)); panel.add(panel_1, BorderLayout.CENTER); panel_1.setLayout(new BorderLayout(0, 0)); //Add buttons to keypad panel // First row jplButtons.add(but7); jplButtons.add(but8); jplButtons.add(but9); // add button / and sqrt jplButtons.add(but13); jplButtons.add(but17); // Second row jplButtons.add(but4); jplButtons.add(but5); jplButtons.add(but6); // add button * and x^2 jplButtons.add(but14); jplButtons.add(but18); // Third row jplButtons.add(but1); jplButtons.add(but2); jplButtons.add(but3); //adds button - and % jplButtons.add(but15); jplButtons.add(but19); //Fourth Row // add 0, +/-, ., +, and = jplButtons.add(but0); jplButtons.add(but10); jplButtons.add(but11); jplButtons.add(but16); jplButtons.add(but12); //Add jPanel jplButtons to jPanel panel_1 panel_1.add(jplButtons, BorderLayout.CENTER); //jplControl is responsible for Backspace, CE, C jplControl = new JPanel(); FlowLayout flowLayout = (FlowLayout) jplControl.getLayout(); flowLayout.setAlignment(FlowLayout.RIGHT); jplControl.setBorder(new EmptyBorder(6, 0, 0, 0)); //jplControl.setBackground(Color.red); panel_1.add(jplControl, BorderLayout.NORTH); //buttons inside the jplControl but20 = new JButton("Backspace"); but20.setMargin(new Insets(3, 1, 3, 1)); but20.setFont(f12); but20.setForeground(Color.red); but20.setBackground(new Color(242,243,238)); but20.addActionListener(this); but21 = new JButton("CE"); but21.setMargin(new Insets(3, 18, 3, 18)); but21.setFont(f121); but21.setForeground(Color.red); but21.setBackground(new Color(242,243,238)); but21.addActionListener(this); but22 = new JButton("C"); but22.setMargin(new Insets(3, 21, 3, 21)); but22.setFont(f121); but22.setForeground(Color.red); but22.setBackground(new Color(242,243,238)); but22.addActionListener(this); JLabel emptySpace = new JLabel(" "); jplControl.add(MemoryTextfield); // jplControl.add(emptySpace); jplControl.add(emptySpace); jplControl.add(but20); jplControl.add(but21); jplControl.add(but22); //jplControl2 is responsible for MC, MR, MS, M+ JPanel memoryPanel = new JPanel(); memoryPanel.setBorder(new EmptyBorder(2, 0, 0, 0)); JPanel jplControl2 = new JPanel(new GridLayout(4, 1, 7, 7)); panel_1.add(jplControl2, BorderLayout.WEST); jplControl2.setBorder(new EmptyBorder(3, 0, 0, 0)); JButton butM1 = new JButton("MC"); butM1.setMargin(new Insets(9, 4, 8, 4)); butM1.setFont(f121); butM1.setForeground(Color.red); butM1.setBackground(new Color(242,243,238)); JButton butM2 = new JButton("MR"); butM2.setMargin(new Insets(9, 4, 8, 4)); butM2.setFont(f121); butM2.setForeground(Color.red); butM2.setBackground(new Color(242,243,238)); JButton butM3 = new JButton("MS"); butM3.setMargin(new Insets(9, 4, 8, 4)); butM3.setFont(f121); butM3.setForeground(Color.red); butM3.setBackground(new Color(242,243,238)); JButton butM4 = new JButton("M+"); butM4.setMargin(new Insets(9, 4, 8, 4)); butM4.setFont(f121); butM4.setForeground(Color.red); butM4.setBackground(new Color(242,243,238)); jplControl2.add(butM1); jplControl2.add(butM2); jplControl2.add(butM3); jplControl2.add(butM4); } public void actionPerformed(ActionEvent e){ if(e.getSource() == but1) { addDigitToDisplay(1); } else if(e.getSource() == but2) { addDigitToDisplay(2); } else if(e.getSource() == but3) { addDigitToDisplay(3); } else if(e.getSource() == but4) { addDigitToDisplay(4); } else if(e.getSource() == but5) { addDigitToDisplay(5); } else if(e.getSource() == but6) { addDigitToDisplay(6); } else if(e.getSource() == but7) { addDigitToDisplay(7); } else if(e.getSource() == but8) { addDigitToDisplay(8); } else if(e.getSource() == but9) { addDigitToDisplay(9); } else if(e.getSource() == but0) { addDigitToDisplay(0); } // +/- else if(e.getSource() == but10) { processSignChange(); } // decimal point else if(e.getSource() == but11) { addDecimalPoint(); } // = else if(e.getSource() == but12) { processEquals(); } // divide else if(e.getSource() == but13) { processOperator("/"); } /* // * else if(e.getSource() == but14) { //processOperator(but14); } // - else if(e.getSource() == but15) { //processOperator(but15); } // + else if(e.getSource() == but16) { //processOperator(but16); } // sqrt else if(e.getSource() == but17) { if (displayMode != ERROR_MODE) { try { if (getDisplayString().indexOf("-") == 0) displayError("Invalid input for function!"); result = Math.sqrt(getNumberInDisplay()); displayResult(result); } catch(Exception ex) { displayError("Invalid input for function!"); displayMode = ERROR_MODE; } } } // 1/x else if(e.getSource() == but18) { if (displayMode != ERROR_MODE){ try { if (getNumberInDisplay() == 0) displayError("Cannot divide by zero!"); result = 1 / getNumberInDisplay(); displayResult(result); } catch(Exception ex) { displayError("Cannot divide by zero!"); displayMode = ERROR_MODE; } } } // % else if(e.getSource() == but19) { if (displayMode != ERROR_MODE){ try { result = getNumberInDisplay() / 100; displayResult(result); } catch(Exception ex) { displayError("Invalid input for function!"); displayMode = ERROR_MODE; } } } // backspace else if(e.getSource() == but20) { if (displayMode != ERROR_MODE){ setDisplayString(getDisplayString().substring(0, getDisplayString().length() - 1)); if (getDisplayString().length() < 1) setDisplayString("0"); } } // CE else if(e.getSource() == but21) { clearExisting(); } // C else if(e.getSource() == but22) { clearAll(); } */ } void setDisplayString(String s){ LabelOutPut.setText(s); } String getDisplayString (){ return LabelOutPut.getText(); } void addDigitToDisplay(int digit){ if (clearOnNextDigit) setDisplayString(""); String inputString = getDisplayString(); if (inputString.indexOf("0") == 0){ inputString = inputString.substring(1); } if ((!inputString.equals("0") || digit > 0) && inputString.length() < MAX_INPUT_LENGTH){ setDisplayString(inputString + digit); } displayMode = INPUT_MODE; clearOnNextDigit = false; } void addDecimalPoint(){ displayMode = INPUT_MODE; if (clearOnNextDigit) setDisplayString(""); String inputString = getDisplayString(); // If the input string already contains a decimal point, don't // do anything to it. if (inputString.indexOf(".") < 0) setDisplayString(new String(inputString + ".")); } void processSignChange(){ if (displayMode == INPUT_MODE) { String input = getDisplayString(); if (input.length() > 0 && !input.equals("0")) { if (input.indexOf("-") == 0) setDisplayString(input.substring(1)); else setDisplayString("-" + input); } } else if (displayMode == RESULT_MODE) { double numberInDisplay = getNumberInDisplay(); if (numberInDisplay != 0) displayResult(-numberInDisplay); } } double getNumberInDisplay() { String input = LabelOutPut.getText(); return Double.parseDouble(input); } void processOperator(String op) { if (displayMode != ERROR_MODE) { double numberInDisplay = getNumberInDisplay(); if (!lastOperator.equals("0")) { try { double result = processLastOperator(); displayResult(result); lastNumber = result; } catch (DivideByZeroException e) { } } else { lastNumber = numberInDisplay; } clearOnNextDigit = true; lastOperator = op; } } void displayResult(double result){ setDisplayString(Double.toString(result)); lastNumber = result; displayMode = RESULT_MODE; clearOnNextDigit = true; } void processEquals(){ double result = 0; if (displayMode != ERROR_MODE){ try { result = processLastOperator(); displayResult(result); } catch (DivideByZeroException e) { displayError("Cannot divide by zero!"); } lastOperator = "0"; } } double processLastOperator() throws DivideByZeroException { double result = 0; double numberInDisplay = getNumberInDisplay(); if (lastOperator.equals("/")) { if (numberInDisplay == 0) throw (new DivideByZeroException()); result = lastNumber / numberInDisplay; } if (lastOperator.equals("*")) result = lastNumber * numberInDisplay; if (lastOperator.equals("-")) result = lastNumber - numberInDisplay; if (lastOperator.equals("+")) result = lastNumber + numberInDisplay; return result; } void displayError(String errorMessage){ setDisplayString(errorMessage); lastNumber = 0; displayMode = ERROR_MODE; clearOnNextDigit = true; } public static void main(String Args[]){ Calculator calc = new Calculator(); calc.setTitle("Calculator"); calc.pack(); calc.setLocation(400, 250); calc.setVisible(true); calc.setSize(270,265); calc.setResizable(true); calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add panel and Menu bar to frame } } class DivideByZeroException extends Exception{ public DivideByZeroException() { super(); } public DivideByZeroException(String s) { super(s); } }