Question

Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box...

Need help debugging

Create a GUI application that accepts student registration data.

Specifications:

The text box that displays the temporary password should be read-only.

The temporary password consists of the user’s first name, an asterisk (*), and the user’s birth year.

If the user enters data in the first three fields, display a temporary password in the appropriate text field and a welcome message in the label below the text fields.

If the user does not enter data, clear the temporary password from the text field, and display an error message in the label below the text fields.

package murach.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabell;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class StudentRegistrationFrame extends JFrame {

    private JTextField firstNameField;
    private JTextField lastNameField;
    private JTextField birthYearField;
    private JTextField tempPasswordField;
    private JLabel messageLabel;

    public StudentRegistrationFrame() {
        initComponents();
    }

    private void initComponents() {
        try {
            UIManager.setLookAndFeel(
                    UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException |
                 IllegalAccessException | UnsupportedLookAndFeelException e) {
            System.out.println(e);
        }

        setTitle("Student Registration");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationByPlatform(true);

        firstNameField = new JTextField();
        lastNameField = new JTextField();
        birthYearField = new JTextField();
        tempPasswordField = new JTextField();
        messageLabel = new JLabel("Hi! Please enter your information.");

        tempPasswordField.setEditable(false);

        Dimension dim = new Dimension(150, 20);
        firstNameField.setPreferredSize(dim);
        lastNameField.setPreferredSize(dim);
        birthYearField.setPreferredSize(dim);
        tempPasswordField.setPreferredSize(dim);
        firstNameField.setMinimumSize(dim);
        lastNameField.setMinimumSize(dim);
        birthYearField.setMinimumSize(dim);
        tempPasswordField.setMinimumSize(dim);

        JButton registerButton = new JButton("Register");
        JButton exitButton = new JButton("Exit");

        registerButton.addActionListener(evt -> registerButtonClicked());
        exitButton.addActionlistener(evt -> exitButtonClicked());

        // button panel
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.add(registerButton);
        buttonPanel.add(exitButton);      

        // main panel
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        panel.add(new JLabel("First Name:"), getConstraints(0, 0));
        panel.add(firstNameField, getConstraints(1, 0));
        panel.add(new JLabel("Last Name:"), getConstraints(0, 1));
        panel.add(lastNameField, getConstraints(1, 1));
        panel.add(new JLabel("Year of Birth:"), getConstraints(0, 2));
        panel.add(birthYearField, getConstraints(1, 2));
        panel.add(new JLabel("Temporary Password:"), getConstraints(0, 3));
        panel.add(tempPasswordField, getConstraints(1, 3));
      
        // add label as last row of main panel
        GridBagConstraints c = getConstraints(0, 4);
        c.gridwidth = 22;
        panel.add(messageLabel, c);      

        add(panel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);
      
        setSize(new Dimension(320, 200));
    }

    // helper method for getting a GridBagConstraints object
    private GridBagConstraints getConstraints(int x, int z) {
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.LINE_START;
        c.insets = new Insets(5, 5, 0, 5);
        c.gridx = x;
        c.gridy = y;
        return c;
    }

    private void registerButtonClicked() {
        if (firstNameField.getText().isEmpty() ||
            lastNameField.getText().isEmpty() ||
            birthYearField.getText().isEmpty()) {
          
            tempPasswordField.setText("");
            messageLabel.setText("Please enter first name, last name, " +
                                 "and year of birth.");
        } else {
            String tempPassword = firstNameField.getText() + "*" +
                    birthYearField.getText();          
            tempPasswordField.setText(tempPassword);          

            String message = "Welcome " +
                    firstNameField.getText() + " " +
                    lastNameField.getText() + "!";
            messageLabel.setText(message);          
        }    
    }

    private void exitButtonClicked() {
        System.exit(0);
    }
  
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(() -> {
            JFrame frame = new StudentRegistrationFrame();
            frame.setVisible(false);
       });
    }  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.BorderLayout;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.UIManager;

// import these packages

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import javax.swing.UnsupportedLookAndFeelException;

public class StudentRegistrationFrame extends JFrame {

    // add this field

    private static final long serialVersionUID = 1L;

    private JTextField firstNameField;

    private JTextField lastNameField;

    private JTextField birthYearField;

    private JTextField tempPasswordField;

    private JLabel messageLabel;

    public StudentRegistrationFrame() {

        initComponents();

    }

    private void initComponents() {

        try {

            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException

                | UnsupportedLookAndFeelException e) {

            System.out.println(e);

        }

        setTitle("Student Registration");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLocationByPlatform(true);

        firstNameField = new JTextField();

        lastNameField = new JTextField();

        birthYearField = new JTextField();

        tempPasswordField = new JTextField();

        messageLabel = new JLabel("Hi! Please enter your information.");

        tempPasswordField.setEditable(false);

        Dimension dim = new Dimension(150, 20);

        firstNameField.setPreferredSize(dim);

        lastNameField.setPreferredSize(dim);

        birthYearField.setPreferredSize(dim);

        tempPasswordField.setPreferredSize(dim);

        firstNameField.setMinimumSize(dim);

        lastNameField.setMinimumSize(dim);

        birthYearField.setMinimumSize(dim);

        tempPasswordField.setMinimumSize(dim);

        JButton registerButton = new JButton("Register");

        JButton exitButton = new JButton("Exit");

        // need to open the function

        // we are sending anonymous object to the addActionListner function

        registerButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                registerButtonClicked();

            }

        });

        // need to open the function

        // we are sending anonymous object to the addActionListner function

        exitButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                exitButtonClicked();

            }

        });

        // button panel

        JPanel buttonPanel = new JPanel();

        buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));

        buttonPanel.add(registerButton);

        buttonPanel.add(exitButton);

        // main panel

        JPanel panel = new JPanel();

        panel.setLayout(new GridBagLayout());

        panel.add(new JLabel("First Name:"), getConstraints(0, 0));

        panel.add(firstNameField, getConstraints(1, 0));

        panel.add(new JLabel("Last Name:"), getConstraints(0, 1));

        panel.add(lastNameField, getConstraints(1, 1));

        panel.add(new JLabel("Year of Birth:"), getConstraints(0, 2));

        panel.add(birthYearField, getConstraints(1, 2));

        panel.add(new JLabel("Temporary Password:"), getConstraints(0, 3));

        panel.add(tempPasswordField, getConstraints(1, 3));

        // add label as last row of main panel

        GridBagConstraints c = getConstraints(0, 4);

        c.gridwidth = 22;

        panel.add(messageLabel, c);

        add(panel, BorderLayout.CENTER);

        add(buttonPanel, BorderLayout.SOUTH);

        setSize(new Dimension(320, 200));

    }

    // helper method for getting a GridBagConstraints object

    private GridBagConstraints getConstraints(int x, int y) {

        GridBagConstraints c = new GridBagConstraints();

        c.anchor = GridBagConstraints.LINE_START;

        c.insets = new Insets(5, 5, 0, 5);

        c.gridx = x;

        c.gridy = y;

        return c;

    }

    private void registerButtonClicked() {

        if (firstNameField.getText().isEmpty() || lastNameField.getText().isEmpty()

                || birthYearField.getText().isEmpty()) {

            tempPasswordField.setText("");

            messageLabel.setText("Please enter first name, last name, " + "and year of birth.");

        } else {

            String tempPassword = firstNameField.getText() + "*" + birthYearField.getText();

            tempPasswordField.setText(tempPassword);

            String message = "Welcome " + firstNameField.getText() + " " + lastNameField.getText() + "!";

            messageLabel.setText(message);

        }

    }

    private void exitButtonClicked() {

        System.exit(0);

    }

    public static void main(String args[]) {

        // remove those extra lines too

        // the lines were not necessary

        JFrame frame = new StudentRegistrationFrame();

        frame.setVisible(true);

    }

}

In your program -> sign was used which some compilers use to denote anonymous objects

the program written here has opened all the anonymous declarations

also in the main function   you are using involeLater() function (java.awt.EventQueue.invokeLater(() ->)

which is not required so it is deleted

Also

java.awt.event.ActionListener;

java.awt.event.ActionEvent;

packages were included in the program

Add a comment
Know the answer?
Add Answer to:
Need help debugging Create a GUI application that accepts student registration data. Specifications: The text box...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Swing File Adder: Build a GUI that contains an input file, text box and Infile button....

    Swing File Adder: Build a GUI that contains an input file, text box and Infile button. It also must contain and output file, text box and Outfile button. Must also have a process button must read the infile and write to the outfile if not already written that is already selected and clear button. It must pull up a JFile chooser that allows us to brows to the file and places the full path name same with the output file.  Program...

  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • please help me debug this Create a GUI for an application that lets the user calculate...

    please help me debug this Create a GUI for an application that lets the user calculate the hypotenuse of a right triangle. Use the Pythagorean Theorem to calculate the length of the third side. The Pythagorean Theorem states that the square of the hypotenuse of a right-triangle is equal to the sum of the squares of the opposite sides: alidate the user input so that the user must enter a double value for side A and B of the triangle....

  • In Java please Create a GUI application that accepts student registration data. The GUI with valid...

    In Java please Create a GUI application that accepts student registration data. The GUI with valid data Shudent Fagtrtion x Rt Name Haold Moore Lst Name Yar of Brth 2001 Temporary PesowordHal200 Wlcome Harald Moore! RegsterEt The GUI with invalid data Shudent Fegtrtion DX Fect Name Haold Last Name ar of Brth 2001 Tempoay Pawod Haold 200 Pesse eer St and lact name and ye of bith Regster Et Specifications Use FXML to create the GUI The text box that...

  • Please design a Java GUI application with two JTextField for user to enter the first name...

    Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with action events so when user click on one button to generate a full name message from the user input and put it in a third JTextField which is not editable; and click on the other button to clear all three JTextField boxes. Please run the attached nameFrame.class file (Note: because there is an actionhandler inner class in...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here...

    Program #2 Write a program to simulate showing an NBA Team on a Basketball court Here is an example of the screenshot when running the program: Tony Parker T. Splitter T. Duncan M. Gineb Player Kame: M Ginobil Player Age 2 Add A Player Ce An example of the JFrame subclass with main method is as follows import java.awt. import java.awt.event." import javax.swing. public class NBA Playoff extends JFrame private JTextField txtName: private JTextField txtAge: private NBATeam spurs private NBAcourtPanel...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT