/* This is a GUI atm program Pin is 111 * */ import javax.swing.*; import java.awt.event.*; import java.awt.*; // Needed for the layout class public class GuiAtm extends JFrame { JPanel numpanel; JPanel textpanel; JPanel mainpanel; JButton one; JButton two; JButton zero; JButton three; JButton four; JButton five; JButton six; JButton seven; JButton eight; JButton nine; JButton enter; JButton clear; JTextArea textarea; boolean value = true; public GuiAtm() { // Sets the title setTitle("Enter Pin Number"); // Sets the size of the window setSize(300, 200); // Sets the location of the frame to middle setLocationRelativeTo(null); // Sets the action when the window is close setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Creates a text area textarea = new JTextArea("", 1, 15); // Create the number buttons 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"); enter = new JButton("Enter"); clear = new JButton("Clear"); // Create three panels mainpanel = new JPanel(); numpanel = new JPanel(); textpanel = new JPanel(); // Add the text area to textpanel textpanel.add(textarea); // Sets the layout for the main panel mainpanel.setLayout(new BorderLayout()); // Sets the layout for the number panel numpanel.setLayout(new FlowLayout(FlowLayout.CENTER)); // Add the buttons to number panel numpanel.add(zero); numpanel.add(one); numpanel.add(two); numpanel.add(three); numpanel.add(four); numpanel.add(five); numpanel.add(six); numpanel.add(seven); numpanel.add(eight); numpanel.add(nine); numpanel.add(clear); numpanel.add(enter); // Adds textpanel and number panel to the main panel mainpanel.add(textpanel, BorderLayout.NORTH); mainpanel.add(numpanel, BorderLayout.CENTER); // Adds the main panel to the content pane add(mainpanel); // Create an event listener for the buttons zero.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()); enter.addActionListener(new ButtonListener()); clear.addActionListener(new ButtonListener()); // Set the visibility of the window setVisible(value); } public class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { try { // Append texts to text area if a number button is clicked 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"); } // Clears everything on the text area if(e.getSource() == clear) { textarea.setText(""); } if(e.getSource()==enter) { // Convert the Strings into integer int num = Integer.parseInt(textarea.getText()); /* If pin matches call the main atm class Then set the value to be false So the window would disappear With only the main atm class window appear * */ if(num==111) { value = false; setVisible(value); MainAtm matm = new MainAtm(); } // If pin doesn't match ask the user to re-enter pin else { JOptionPane.showMessageDialog(null, "Pin does not match\n Please re-enter"); } } } catch(NumberFormatException ae) { } } } public static void main(String[] args) { new GuiAtm(); } }