Guest User

loginGUIupdated

a guest
May 3rd, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. import java.io.*; // til filbehandling
  2. import java.util.*; // til scanner
  3. import java.awt.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6.  
  7. // FRAME
  8.  
  9. public class loginLayout extends JFrame implements ActionListener {
  10.    
  11.     static JButton loginButton;
  12.     static JTextField usernameField;
  13.     static JTextField passwordField;
  14.            
  15.     public static void main(String[] args) throws FileNotFoundException {
  16.         loginLayout gui = new loginLayout();
  17.    
  18.     }
  19.    
  20.     public loginLayout() {
  21.         Scanner console = new Scanner(System.in); // Scanner
  22.         final loginCheck getData = new loginCheck(); // Objekt getData metoder fra loginCheck.java
  23.  
  24.        
  25.        
  26.         // Gui vinduet hentes
  27.         JFrame frame = new JFrame();
  28.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  29.         frame.setTitle("Dolphin system login");
  30.         frame.setLayout(new BorderLayout());
  31.        
  32.        
  33.            
  34.             // Felterne til login
  35.             JPanel textPanel = new JPanel(new GridLayout(2,2));
  36.            
  37.                 final JTextField typedCPR = new JTextField("", 10);
  38.                 typedCPR.setPreferredSize(new Dimension(120, 20));
  39.                 final JTextField typedPassword = new JTextField("", 10);
  40.                 typedPassword.setPreferredSize(new Dimension(120, 20));
  41.                
  42.                     textPanel.add(new JLabel("Username: "));
  43.                     textPanel.add(typedCPR);
  44.                     textPanel.add(new JLabel("Password: "));
  45.                     textPanel.add(typedPassword);
  46.                    
  47.                     frame.add(textPanel, BorderLayout.NORTH);
  48.        
  49.        
  50.        
  51.             // Knap til login
  52.             JPanel buttonPanel = new JPanel(new GridLayout(1,2));
  53.                 JButton loginButton = new JButton();
  54.                 loginButton.setText("Log in");
  55.                 loginButton.setPreferredSize(new Dimension(10, 20));
  56.                
  57.                
  58.                 // Prøver at logge ind og viser indhold hvis
  59.                 // indtastede data er korrekt (Bruger en ActionListener)
  60.                     loginButton.addActionListener(new ActionListener() {
  61.                    
  62.                         public void actionPerformed(ActionEvent e) {
  63.                        
  64.                         String cprTry = typedCPR.getText();
  65.                         String passwordTry = typedPassword.getText();
  66.                        
  67.                         String cpr = getData.getLogin(cprTry, passwordTry);
  68.                                
  69.                             if (cpr.equals("dontexist")) {
  70.                                 System.out.println("Brugernavnet eksisterer ikke i databasen");
  71.                             }
  72.                             else if (cpr.equals("wrongpass")) {
  73.                                 System.out.println("Brugernavn og password passer ikke sammen!");
  74.                             }
  75.                             else {
  76.                            
  77.                             }
  78.                         }
  79.                     });
  80.                    
  81.                    
  82.                     buttonPanel.add(new JLabel(""));
  83.                     buttonPanel.add(loginButton);
  84.                
  85.                     frame.add(buttonPanel, BorderLayout.SOUTH);
  86.            
  87.            
  88.            
  89.            
  90.         frame.pack();
  91.         frame.setLocationRelativeTo(null);
  92.         frame.setVisible(true);    
  93.        
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment