Advertisement
mrScarlett

register Screen

May 23rd, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class registerScreen extends JFrame{
  6.  
  7.   // setting up the variables for the GUI items
  8.   private JTextField _username = new JTextField(7); // text field for username
  9.   private JTextField _usernameAgain = new JTextField(7); // text field for user name
  10.  
  11.   // 1. Create text fields for password and password again
  12.  
  13.   private JLabel _result = new JLabel(""); // a label to display the final result
  14.  
  15.   private JButton enterBtn = new JButton("Submit"); //a button to submit the answers
  16.  
  17.   public registerScreen() {
  18.      
  19.     JFrame frame = new JFrame("Register User"); // defining a new frame for the window.
  20.    
  21.     frame.setLayout(new GridLayout(0, 2)); //using a Grid layout 0 rows 2 columns
  22.    
  23.     //adding items to the frame, when the columns in a row are full then it shall move onto the next row.
  24.     frame.add(new JLabel("Register Details."));//row 1
  25.     frame.add(new JLabel(""));//row 1
  26.    
  27.    
  28.     frame.add(new JLabel("Enter Name"));//row 2
  29.     frame.add(_username);//row 2
  30.     frame.add(new JLabel("Enter Name again"));//row 2
  31.     frame.add(_usernameAgain);//row 2
  32.    
  33.     //2. Use frame.add to add the password and password again text fields, include suitable labels too.
  34.    
  35.    
  36.     frame.add(enterBtn);
  37.     frame.add(_result);
  38.    
  39.     frame.pack();
  40.    
  41.     frame.setVisible(true);
  42.    
  43.    //.addActionListener links the button to the SubmitButton class, that will check the values
  44.    //when the submit button is clicked
  45.     enterBtn.addActionListener(new SubmitBtnListener());
  46.     _username.addActionListener(new SubmitBtnListener());
  47.     _usernameAgain.addActionListener(new SubmitBtnListener());
  48.     //3.  link the password and passwordAgain text fields to the action listener.
  49.    
  50.    
  51.   }
  52.  
  53.   //code that runs when the button is clicked
  54.   class SubmitBtnListener implements ActionListener {        
  55.         public void actionPerformed(ActionEvent e) {
  56.             //new variables created to store the values in the username and usernameAgain text fields
  57.             String usernameStr = _username.getText();
  58.             String usernameAgainStr = _usernameAgain.getText();
  59.            
  60.           //4.Create 2 more variables to store the values input into the password and passwordAgain text fields
  61.            
  62.           //5. Develop the code below using && to check if the passwords entered are the same.
  63.             if (usernameStr.equals(usernameAgainStr)){
  64.                 _result.setText("Match");//changes the result label to 'Match' if the usernames are the same
  65.                 //LINK TO QUIZ HERE
  66.                
  67.             } else{
  68.                 _result.setText("Non Match");//changes the result label to X if the answer is incorrect
  69.             }
  70.            
  71.         }
  72.     }
  73.  
  74.   public static void main(String[] args) {
  75.         registerScreen window = new registerScreen();
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement