my login form using java awt-swing ..still appearing after clicking submit button when i entered into next page how can we make it invisible
Hello Friend,
We have provided you the code where if user enters correct username and password then the frame will get closed and only welcome page display. In case if user enters incorrect username or password then login page will not closed and message will get displayed.
To make the login frame invisible, we have used f.setVisible(false).
We are again sending you the code:
1)Login.java:
import javax.swing.*; import java.sql.*; import java.awt.*; import java.awt.event.*; class Login { JButton SUBMIT; JLabel label1,label2; final JTextField text1,text2; Login() { final JFrame f=new JFrame("Login Form"); f.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); f.add(label1); f.add(text1); f.add(label2); f.add(text2); f.add(SUBMIT); f.setVisible(true); f.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)) { f.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){} } }); } } class LoginDemo { public static void main(String arg[]){ new Login(); } }
2)NextPage.java:
import javax.swing.*; import java.awt.*; class NextPage extends JFrame { NextPage(String st) { setLayout(null); setDefaultCloseOperation(javax.swing. WindowConstants.DISPOSE_ON_CLOSE); setTitle("Welcome"); JLabel lab=new JLabel("Welcome "+st); lab.setBounds(10,10,500,20); add(lab); setSize(1024, 768); } }
If you have made some changes in the above code, then post your query with code.
Thanks
Ads