/* This is a more advance calculator that * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AdvanceCalculator extends JFrame { JTextArea textarea; double currentTotal = 0.0; char choice; JPanel mainpanel; JPanel buttonpanel; JButton clear; JButton zero; JButton one; JButton two; JButton three; JButton four; JButton five; JButton six; JButton seven; JButton eight; JButton nine; JButton decimal; JButton plus; JButton equal; JButton times; JButton divide; public AdvanceCalculator() { // Sets the window title setTitle("Advance Calculator"); // Sets the size of the window setSize(300, 400); // Sets the start location of the window setLocationRelativeTo(null); // Sets the action when window is close setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Sets the length of the text field textarea = new JTextArea("", 1,13); //Creates a panel using GridLayout buttonpanel = new JPanel(); buttonpanel.setLayout(new java.awt.GridLayout(10,2)); mainpanel = new JPanel(); mainpanel.setLayout(new BorderLayout()); //create the buttons clear = new JButton("Clear"); zero = new JButton("0"); one = new JButton("1"); two = new JButton("2"); three = new JButton("3"); four = new JButton("4"); five = new JButton("5"); six = new JButton("6"); seven = new JButton("7"); eight = new JButton("8"); nine = new JButton("9"); decimal = new JButton("."); plus = new JButton("+"); times = new JButton("X"); divide = new JButton("/"); equal = new JButton("="); //sets the actionlistener clear.addActionListener(new ButtonListener()); one.addActionListener(new ButtonListener()); two.addActionListener(new ButtonListener()); three.addActionListener(new ButtonListener()); four.addActionListener(new ButtonListener()); five.addActionListener(new ButtonListener()); six.addActionListener(new ButtonListener()); seven.addActionListener(new ButtonListener()); eight.addActionListener(new ButtonListener()); nine.addActionListener(new ButtonListener()); zero.addActionListener(new ButtonListener()); plus.addActionListener(new ButtonListener()); equal.addActionListener(new ButtonListener()); decimal.addActionListener(new ButtonListener()); times.addActionListener(new ButtonListener()); divide.addActionListener(new ButtonListener()); //adds the button to the panel buttonpanel.add(zero); buttonpanel.add(one); buttonpanel.add(two); buttonpanel.add(three); buttonpanel.add(four); buttonpanel.add(five); buttonpanel.add(six); buttonpanel.add(seven); buttonpanel.add(eight); buttonpanel.add(nine); buttonpanel.add(times); buttonpanel.add(plus); buttonpanel.add(divide); buttonpanel.add(decimal); buttonpanel.add(equal); buttonpanel.add(clear); // Adds the buttonpanel to the main panel mainpanel.add(buttonpanel, BorderLayout.CENTER); mainpanel.add(textarea, BorderLayout.NORTH); // Adds the mainpanel to the content pane add(mainpanel); // Sets the visibility of the window setVisible(true); } public class ButtonListener implements ActionListener { // This is the action it will perform when a button is pushed public void actionPerformed(ActionEvent e) { // Append texts to text area if a number button is clicked try { if(e.getSource( )== zero) { textarea.append("0"); } if(e.getSource( )== one) { textarea.append("1"); } if(e.getSource() == two) { textarea.append("2"); } if(e.getSource() == three) { textarea.append("3"); } if(e.getSource() == four) { textarea.append("4"); } if(e.getSource() == five) { textarea.append("5"); } if(e.getSource() == six) { textarea.append("6"); } if(e.getSource() == seven) { textarea.append("7"); } if(e.getSource() == eight) { textarea.append("8"); } if(e.getSource() == nine) { textarea.append("9"); } if(e.getSource() == decimal) { textarea.append("."); } // Addition is done in here if(e.getSource()== plus) { currentTotal += Double.parseDouble(textarea.getText()); textarea.setText(""); choice = '+'; } // Mulitplication is done in here if(e.getSource()==times) { if( currentTotal == 0.0 ) { currentTotal = 1; currentTotal *= Double.parseDouble(textarea.getText()); textarea.setText(""); choice = 'x'; } else { currentTotal *= Double.parseDouble(textarea.getText()); textarea.setText(""); choice = 'x'; } } if(e.getSource() == divide) { currentTotal = Double.parseDouble(textarea.getText()); textarea.setText(""); choice = '/'; } // Outputs the result if(e.getSource() == equal) { if(choice == '+') { currentTotal += Double.parseDouble(textarea.getText()); } if(choice == 'x') { currentTotal *= Double.parseDouble(textarea.getText()); } if(choice =='/') { double num = Double.parseDouble(textarea.getText()); if(num==0) { JOptionPane.showMessageDialog(null,"Error, dividing by zero"); } else { currentTotal /= Double.parseDouble(textarea.getText()); } } textarea.setText(""+currentTotal); } // Clears everything on the text area if(e.getSource() == clear) { textarea.setText(""); currentTotal=0.0; } } catch(NumberFormatException ae) { } } } public static void main(String[] args) { new AdvanceCalculator(); } }