Simple Calculator
/* This is a simple calculator that only does single digit addition It asks the user for a username first, if the user name is incorrect The program will not start */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class Calculator extends JFrame implements ActionListener { JTextField text; int currentTotal; String displaytext; public Calculator() { //creates a container Container cp = getContentPane(); /* we will handles all the exception It will use a while loop to ask the user for a user name If it is incorrect, the program will not start * */ try{ String name = JOptionPane.showInputDialog("Please enter your name"); cp.setLayout(new BorderLayout()); while(!name.equals("jj")) { name = JOptionPane.showInputDialog("Please enter your name"); } //Sets the length of the text field text = new JTextField(12); //Creates a panel using GridLayout JPanel panel = new JPanel(new GridLayout(10,2)); //create the buttons JButton c = new JButton("Clear"); JButton zero = new JButton("0"); JButton one = new JButton("1"); JButton two = new JButton("2"); JButton three = new JButton("3"); JButton four = new JButton("4"); JButton five = new JButton("5"); JButton six = new JButton("6"); JButton seven = new JButton("7"); JButton eight = new JButton("8"); JButton nine = new JButton("9"); JButton plus = new JButton("+"); JButton equal = new JButton("="); //sets the actionlistener c.addActionListener(this); one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); five.addActionListener(this); six.addActionListener(this); seven.addActionListener(this); eight.addActionListener(this); nine.addActionListener(this); zero.addActionListener(this); plus.addActionListener(this); equal.addActionListener(this); //adds the text and panel using borderlayout add(text, BorderLayout.NORTH); add(panel, BorderLayout.CENTER); //adds the button to the panel panel.add(zero); panel.add(one); panel.add(two); panel.add(three); panel.add(four); panel.add(five); panel.add(six); panel.add(seven); panel.add(eight); panel.add(nine); panel.add(plus); panel.add(plus); panel.add(equal); panel.add(c); } catch(Exception ae) { } } //Function for clear public void clear() { text.setText(""); currentTotal = 0; } /* here we create multiple functions to do addition * */ public void add(String n) { currentTotal += convert(n); } public double convert(String n) { return Integer.parseInt(n); } public String getTotal() { return ""+currentTotal; } // This is the action it will perform when a button is pushed public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if(ac.equals("Clear")) { clear(); } if(ac.equals("1")) { text.setText("1"); } if(ac.equals("2")) { text.setText("2"); } if(ac.equals("3")) { text.setText("3"); } if(ac.equals("4")) { text.setText("4"); } if(ac.equals("5")) { text.setText("5"); } if(ac.equals("6")) { text.setText("6"); } if(ac.equals("7")) { text.setText("7"); } if(ac.equals("8")) { text.setText("8"); } if(ac.equals("9")) { text.setText("9"); } if(ac.equals("0")) { text.setText("0"); } if(ac.equals("=")) { displaytext = text.getText(); add(displaytext); text.setText(getTotal()); } if(ac.equals("+")) { displaytext = text.getText(); add(displaytext); } } public static void main(String[] args) { Calculator cal = new Calculator(); cal.setSize(300,400); //sets the size of the window cal.setTitle("JJ Simple Calculator");//sets the title of the calculator cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cal.setVisible(true); } }
< Java > < GUI > < Home >