Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import javax.swing.*;
- import java.awt.event.*;
- public class registerScreen extends JFrame{
- // setting up the variables for the GUI items
- private JTextField _username = new JTextField(7); // text field for username
- private JTextField _usernameAgain = new JTextField(7); // text field for user name
- // 1. Create text fields for password and password again
- private JLabel _result = new JLabel(""); // a label to display the final result
- private JButton enterBtn = new JButton("Submit"); //a button to submit the answers
- public registerScreen() {
- JFrame frame = new JFrame("Register User"); // defining a new frame for the window.
- frame.setLayout(new GridLayout(0, 2)); //using a Grid layout 0 rows 2 columns
- //adding items to the frame, when the columns in a row are full then it shall move onto the next row.
- frame.add(new JLabel("Register Details."));//row 1
- frame.add(new JLabel(""));//row 1
- frame.add(new JLabel("Enter Name"));//row 2
- frame.add(_username);//row 2
- frame.add(new JLabel("Enter Name again"));//row 2
- frame.add(_usernameAgain);//row 2
- //2. Use frame.add to add the password and password again text fields, include suitable labels too.
- frame.add(enterBtn);
- frame.add(_result);
- frame.pack();
- frame.setVisible(true);
- //.addActionListener links the button to the SubmitButton class, that will check the values
- //when the submit button is clicked
- enterBtn.addActionListener(new SubmitBtnListener());
- _username.addActionListener(new SubmitBtnListener());
- _usernameAgain.addActionListener(new SubmitBtnListener());
- //3. link the password and passwordAgain text fields to the action listener.
- }
- //code that runs when the button is clicked
- class SubmitBtnListener implements ActionListener {
- public void actionPerformed(ActionEvent e) {
- //new variables created to store the values in the username and usernameAgain text fields
- String usernameStr = _username.getText();
- String usernameAgainStr = _usernameAgain.getText();
- //4.Create 2 more variables to store the values input into the password and passwordAgain text fields
- //5. Develop the code below using && to check if the passwords entered are the same.
- if (usernameStr.equals(usernameAgainStr)){
- _result.setText("Match");//changes the result label to 'Match' if the usernames are the same
- //LINK TO QUIZ HERE
- } else{
- _result.setText("Non Match");//changes the result label to X if the answer is incorrect
- }
- }
- }
- public static void main(String[] args) {
- registerScreen window = new registerScreen();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment