Create an applet that provides a login screen. The initial user interface will h
ID: 3528809 • Letter: C
Question
Create an applet that provides a login screen. The initial user interface will have a JTextField and a JButton. The JTextField will be used to enter a password. It will initially be blank. Clicking the JButton or pressing the Enter key from the JTextField will initiate the comparison of the user entered password to the list of valid passwords. Put an appropriate label on the JButton. Place a JLabel at the top of the applet providing instructions. Make the font and point size used for the JLabel different than that used for the text in the JTextField. Store five acceptable password in an array. The valid passwords are one, two, three, four, and five. Search the array using a loop to determine if the user entered password is valid when the JButton is clicked or the Enter key is hit from the JTextfield. Your applet should ignore case differences when comparing the user entered password with the list of valid passwords. Put your APPLET tag in your source code as comments. Your APPLET tag must be correct as part of this assignment. Display "Access Granted" if the user entered password matches one of the valid passwords. Display "Access Denied" if the user entered password doesn't match one of the valid passwords. Display the message in the applet window using a JLabel not on the monitor (DOS window or output window). Do not display the messages until the user hits the Enter key or clicks the JButton. The messages "Access Granted" or "Access Denied" should appear only once in the applet output. The applet should not display "Access Denied" followed by "Access Granted" when processing an invalid password followed by a valid password. Rather, the "Access Granted" message should replace the "Access Denied" message under these circumstances. Name your class Password.Use the FlowLayout layout manager.Explanation / Answer
import javax.swing.*; import java.sql.*; import java.awt.*; import java.awt.event.*; class LoginDemo extends JFrame { JButton SUBMIT; JLabel label1,label2; final JTextField text1,text2; LoginDemo() { setTitle("Login Form"); setLayout(null); label1 = new JLabel(); label1.setText("Username:"); text1 = new JTextField(15); label2 = new JLabel(); label2.setText("Password:"); text2 = new JPasswordField(15); SUBMIT=new JButton("SUBMIT"); label1.setBounds(350,100,100,20); text1.setBounds(450,100,200,20); label2.setBounds(350,130,100,20); text2.setBounds(450,130,200,20); SUBMIT.setBounds(450,160,100,20); add(label1); add(text1); add(label2); add(text2); add(SUBMIT); setVisible(true); setSize(1024,768); SUBMIT.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { String value1=text1.getText(); String value2=text2.getText(); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from login where username='"+value1+"' and password='"+value2+"'"); String uname="",pass=""; if(rs.next()){ uname=rs.getString("username"); pass=rs.getString("password"); } if (value1.equals(uname) && value2.equals(pass)) { setVisible(false); NextPage page=new NextPage(uname); page.setVisible(true); } else{ JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE); text1.setText(""); text2.setText(""); } } catch(Exception e){} } }); } public static void main(String arg[]){ new LoginDemo(); } } 2)NextPage.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; class NextPage extends JFrame { NextPage(String st) { setLayout(null); setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE); setTitle("Welcome"); JLabel lab=new JLabel("Welcome "+st); JButton b=new JButton("Logout"); lab.setBounds(10,10,500,20); b.setBounds(600,10,100,20); b.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ LoginDemo demo=new LoginDemo(); demo.setVisible(true); setVisible(false); } }); add(lab); add(b); setSize(1024, 768); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.