Create a JFrame Form for new user registration. The registration GUI is shown in
ID: 3826547 • Letter: C
Question
Create a JFrame Form for new user registration. The registration GUI is shown in Fig. (a). The components include two labels (User Name and Password), one text field for user name input, one password field for password input, two buttons (register and cancel), and one label of message. The user actions and their corresponding event listener methods are as follows: User action Listener methods: Click cancel button Xaction Performed (X is the cancel button object name): clear the input in the user name text field, and the password field; also clear the message area. Click register button Xaction Performed (X is the register button object name): call validatePassword to validate the password. If the password is valid, display message "registration successful" in the message area, as shown in Fig. (b) otherwise display an error message of "a password has at least one digit and one letter" in red color in the message area, as shown in Fig. (c) Click mouse on user name field: xmouseClicked (X is the text field object name) clear the text field for user name input (ready for user to reenter name). Click mouse on password field: XmouseClicked (X is the password field object name): clear the password field for password input (ready for user to reenter the password). Press return on password Field: Xaction Performed (X is the password field object name):Same as register button actionPerformed Other private methods validatePassword: if the password contains at least one digit and one letter, return true (password valid), otherwise return false (password invalid). Please turn in the *.java and *.form files.Explanation / Answer
Hi,
Please find below the java code for the User registration-
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Registration extends JFrame implements ActionListener
{
JLabel l1, l2,l3;
JTextField tf1;
JButton btn1, btn2;
JPasswordField p1;
Registration()
{
setVisible(true);
setSize(700, 700);
setLayout(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("New Registration");
l1 = new JLabel("Registration Form in Windows Form:");
l1.setForeground(Color.blue);
l1.setFont(new Font("Serif", Font.BOLD, 20));
l2 = new JLabel("Name:");
l3 = new JLabel("Create Passowrd:");
tf1 = new JTextField();
p1 = new JPasswordField();
btn1 = new JButton("Submit");
btn2 = new JButton("Clear");
btn1.addActionListener(this);
btn2.addActionListener(this);
l1.setBounds(100, 30, 400, 30);
l2.setBounds(80, 70, 200, 30);
l3.setBounds(80, 110, 200, 30);
tf1.setBounds(300, 70, 200, 30);
p1.setBounds(300, 150, 200, 30);
btn1.setBounds(50, 350, 100, 30);
btn2.setBounds(170, 350, 100, 30);
add(l1);
add(l2);
add(tf1);
add(l3);
add(p1);
add(btn1);
add(btn2);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == btn1)
{
int x = 0;
String s1 = tf1.getText();
char[] s2 = p1.getPassword();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@Vinay07:1521:xe", "vinay", "Hello");
PreparedStatement ps = con.prepareStatement("insert into reg values(?,?,?,?,?,?)");
ps.setString(1, s1);
ps.setString(2, s2);
ResultSet rs = ps.executeQuery();
x++;
if (x > 0)
{
JOptionPane.showMessageDialog(btn1, "Data Saved Successfully");
}
}
catch (Exception ex)
{
System.out.println(ex);
}
}
else
{
tf1.setText("");
p1.setText("");
}
}
public static void main(String args[])
{
new Registration();
}
}