Q)Write a program to design an interface containing fields User ID, Password and Account type, and buttons login, cancel, edit by mixing border layout and flow layout. Add events handling to the button login and cancel such that clicking in login checks for matching user id and password in the database and opens another window if login is successful and displays appropriate message if login is not successful. Clicking in cancel terminates our program Let us suppose we have a database named user and a table named user_table with fields user_id and password. Now the complete code for the above task is given below: //UserInterfaceDesign.java import java.awt.*; //needed to create GUI components import javax.swing.*; //needed to create GUI components import java.awt.event.*; //needed to take a action upon any event e.g. clicking on a button import java.sql.*; //needed for database connection /* In order to create a frame, we need to extend Jframe. Extending Jframe means that our class is inherited from Jframe class. Extends is a keyword user for inheritance. We can inherit only one class. */ public class UserInterfaceDesign extends JFrame { //Firstly all the necessary variables are created //Jlabel is used to create label.. This code creates a label named User ID JLabel user_id_label = new JLabel("User ID:"); //this code creates a tetfield of size 10 to enter the user ID JTextField user_id_field = new JTextField(10); //this line creates another label called Password JLabel password_label = new Jlabel("Password:"); //this code creates a tetfield of size 10 to enter the password JPasswordField password_field = new JPasswordField(10); //this line creates another label called Account Type JLabel account_type_label = new JLabel("Account Type:"); //Radio Button is used for Account Type //This code is used to create a radio button with name Savings and Current JRadioButton account_type_radio_button1 = new JRadioButton("Savings");
JRadioButton account_type_radio_button2 = new JRadioButton("Current"); //A new object of ButtonGroup is created. These are created such that only one radio button //could be choosed at a time ButtonGroup account_type_button_group = new ButtonGroup(); //Jbutton is used to create a button. This creates a butto with name Login JButton login_button = new Jbutton("LogIn"); //Creates button with text Cancel JButton cancel_button = new Jbutton("Cancel"); //Creates button with text edit JButton edit_button = new Jbutton("Edit"); /* Required variable declaration is completed. Till now, these GUI components will not be displayed to users as we have only created a object of all the required GUI components. In order to make them visible, we must create a Jpanel and then add all these components to Jpanel. This is done inside the default constructor. */ public UserInterfaceDesign() { //Firstly, the radio buttons are added to a button group such that user can only select one option at a time account_type_button_group.add(account_type_radio_button1); account_type_button_group.add(account_type_radio_button2); //Creates a main panel and set its layout to be FlowLayout with all its child to be left aligned JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT)); // Now all the components are added to the panel p1.add(user_id_label); p1.add(user_id_field); p1.add(password_label);p1.add(password_field); p1.add(account_type_label); p1.add(account_type_radio_button1); p1.add(account_type_radio_button2); //Now, finally, the panel is added to the Jframe with BorderLayout.NORTH such that the panel is places in north direction add(p1,borderlayout.north);
//Now, next panel is created to add all the buttons JPanel p2 = new JPanel(new FlowLayout(50)); //All buttons are added in this panel p2.add(login_button); p2.add(cancel_button); p2.add(edit_button); //Finally, the panel with button is added to Jframe in south direction add(p2,borderlayout.south); //Now, the click events need to be defined. Since click is a action, so firstly, action listener is //added to the button login_button.addactionlistener(new ActionListener() { // In order to perform click event, following function must be overridden @Override public void actionperformed(actionevent e) { try{ // Loads the driver Class.forName("com.mysql.jdbc.Driver"); // Connects java to mysql where jdbc:mysql://localhost:3306/user is the path of the //driver, root is the username and is the password Connection c =DriverManager.getConnection ("jdbc:mysql://localhost:3306/user", "root", ); //Sends every parameter to mysql Statement s = c.createstatement(); /*In order to get the text from the user_id_field textfield, gettext() method is used. This method gets the text written in the textfield. This method returns string by default, hence Integer.parseInt() method is used to convert this string to int and store it in int*/ int id = Integer.parseInt(user_id_field.getText()); /* for password, the process is little different. Since password is not written in plaintext, so getpassword() method is used to get the text of password field. This method writtens char[] by default which is converted to string by String.valueOf() function.*/ char [] pass = password_field.getpassword();
); String passwd = String.valueOf (pass); //ID and password are displayed in console System.out.println("ID:"+id+"\tPassword:"+passwd); //database sql query String sql = "SELECT * FROM user_table WHERE user_id = "+id+" AND password='"+passwd+"'"; //r stores the result set after the sql query is executed ResultSet r = s.executequery(sql); //moves cursor to the first row, i.e. if first row is preset if(r.first()) { JOptionPane.showMessageDialog(new JFrame(), "Log In Successful!!!" ); else { JOptionPane.showMessageDialog(null, "Incorrect UserID/Password"); catch (Exception ex){ System.out.println("Exception occurred"); //Code when cancel button is clicked cancel_button.addactionlistener(new ActionListener() { @Override public void actionperformed(actionevent e) { // exits current program by terminating running Java virtual machine System.exit(0); ); setsize(700,300); //set the size of JFrame setvisible(true); // Finally, making the Jframe visible on user s screen
setdefaultcloseoperation(jframe.exit_on_close); // exit the program on clicking cross button of frame //Main Method public static void main(string []args) { new UserInterfaceDesign();