package guis; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.SwingUtilities; import javax.swing.UIManager; public class LoginDemo extends JFrame { /** * */ private static final long serialVersionUID = 1L; static LoginDemo loginGUI = new LoginDemo(); static JFrame submitGUI = new JFrame(); static HashMap users = new HashMap(); public static void main(String []args){ try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); SwingUtilities.updateComponentTreeUI(loginGUI); SwingUtilities.updateComponentTreeUI(submitGUI); }catch(Exception e){ } users.put("Pete", "petepass"); users.put("Dale", "dalepass"); loginGUI.setResizable(false); loginGUI.setTitle("Please Login"); loginGUI.setPreferredSize(new Dimension(250,120)); loginGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel mainPanel = new JPanel(new GridBagLayout()); buildBasicGUI(mainPanel); loginGUI.add(mainPanel); loginGUI.pack(); loginGUI.setLocation(420, 325); loginGUI.setVisible(true); } public static void buildBasicGUI(JPanel mainPanel){ final JLabel userLabel = new JLabel("User Name: "); final JLabel passwordLabel = new JLabel("Password: "); final JLabel logError = new JLabel(" "); final JTextField userName = new JTextField(12); final JPasswordField password = new JPasswordField(12); final JButton loginButton = new JButton("Login"); loginButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String user = userName.getText(); char pass[] = password.getPassword(); if(pass != null && loginValid(user, pass)){ password.setText(""); logError.setText(" "); loginGUI.setVisible(false); buildAndDisplaySubmit(); } else{ logError.setText("Invalid Login Information!"); password.setText(""); } } }); GridBagConstraints c = new GridBagConstraints(); c.gridx = 1;c.gridy = 0; c.gridwidth = 2; mainPanel.add(logError, c); c.gridwidth = 1; c.gridy++;c.gridx--; mainPanel.add(userLabel, c); c.gridy++; mainPanel.add(passwordLabel, c); c.gridy--;c.gridx++; mainPanel.add(userName, c); c.gridy++; mainPanel.add(password, c); c.gridy++; mainPanel.add(loginButton,c); } public static void buildAndDisplaySubmit(){ JPanel submitPanel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); //Labels final JLabel errors = new JLabel(" "); JLabel orderNoLabel = new JLabel("* Order Number: "); JLabel fNameLabel = new JLabel("* First Name: "); JLabel lNameLabel = new JLabel("* Last Name: "); JLabel phoneLabel = new JLabel(" Phone Number: "); JLabel emailLabel = new JLabel("* E-mail: "); //Text final JTextField orderNo = new JTextField(15); final JTextField fName = new JTextField(15); final JTextField lName = new JTextField(15); final JTextField phone = new JTextField(15); final JTextField email = new JTextField(15); //Button JButton submit = new JButton("Submit"); JButton cancel = new JButton("Logout"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { orderNo.setText(""); fName.setText(""); lName.setText(""); phone.setText(""); email.setText(""); errors.setText(" "); submitGUI.setVisible(false); loginGUI.setVisible(true); }}); submit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ArrayList fields = new ArrayList(); //Might try to do more with errors. thats why its set up like this.. if(orderNo.getText().trim().equals("")){ fields.add("Order Number"); } if(fName.getText().trim().equals("")){ fields.add("First Name"); } if(lName.getText().trim().equals("")){ fields.add("Last Name"); } if(lName.getText().trim().equals("")){ fields.add("Last Name"); } if(email.getText().trim().equals("")){ fields.add("E-mail"); } if(fields.size() > 0){ errors.setText("You are missing at least one Required Field"); } else{ errors.setText("Form Submitted!"); } }}); c.weighty = .25; c.gridx = 0; c.gridy = 0; c.gridwidth = 2; submitPanel.add(errors, c); c.gridwidth = 1; c.gridy++; submitPanel.add(orderNoLabel, c); c.gridx++; submitPanel.add(orderNo, c); c.gridx--;c.gridy++; submitPanel.add(fNameLabel, c); c.gridx++; submitPanel.add(fName, c); c.gridx--;c.gridy++; submitPanel.add(lNameLabel, c); c.gridx++; submitPanel.add(lName, c); c.gridx--;c.gridy++; submitPanel.add(phoneLabel, c); c.gridx++; submitPanel.add(phone, c); c.gridx--;c.gridy++; submitPanel.add(emailLabel, c); c.gridx++; submitPanel.add(email, c); c.gridx--;c.gridy++; submitPanel.add(cancel, c); c.gridx++; submitPanel.add(submit, c); c.gridx--;c.gridy++; submitGUI.setResizable(false); submitGUI.setTitle("Form Submission"); submitGUI.setPreferredSize(new Dimension(380,280)); submitGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); submitGUI.add(submitPanel); submitGUI.pack(); submitGUI.setLocation(420, 325); submitGUI.setVisible(true); } private static boolean loginValid(String user, char pass[]){ String correct = users.get(user); return ((correct != null) && Arrays.equals(pass, correct.toCharArray())); } }