hey i have taken your code and modified it as below. the problem is i am unable to call my login page from my start page. its not opening the login page when i click on login. can anyone help.. please its urgent..
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.sql.*; class Login extends JFrame { JButton SUBMIT,ADD; JPanel panel; JLabel label1,label2; final JTextField text1; final JPasswordField text2; Login(){ label1 = new JLabel(); label1.setText("UserName:"); text1 = new JTextField(15); label2 = new JLabel(); label2.setText("Password:"); text2 = new JPasswordField(15); SUBMIT=new JButton("Login"); ADD=new JButton("Create Account"); panel=new JPanel(new GridLayout(3,2)); panel.add(label1); panel.add(text1); panel.add(label2); panel.add(text2); panel.add(SUBMIT); panel.add(ADD); add(panel,BorderLayout.CENTER); SUBMIT.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ String value1=text1.getText(); String value2=text2.getText(); if (value1 == null || value1.equals("")) { JOptionPane.showMessageDialog(null, "Enter user id", "Missing field", JOptionPane.DEFAULT_OPTION); return; } if (value2 == null || value2.equals("")) { JOptionPane.showMessageDialog(null, "Enter password", "Missing field", JOptionPane.DEFAULT_OPTION); return; } Connection con = null; String url="jdbc:mysql://localhost:3306/"; String db="pharma"; String driver="com.mysql.jdbc.Driver"; String user="root"; String pass="root"; String user1=""; String pass1=""; try { Class.forName(driver); con = DriverManager.getConnection(url+db, user, pass); Statement st = con.createStatement(); ResultSet res = st.executeQuery("SELECT * FROM login where username='"+value1+"' && password='"+value2+"'"); while (res.next()) { user1 = res.getString("username"); pass1 = res.getString("password"); } if (value1.equals(user1) && value2.equals(pass1)) { dispose(); JOptionPane.showMessageDialog(null,"Welcome "+user1+", You have successfully Login"); HcHome h=new HcHome(); h.setBounds(0,0,1020,1000); h.setVisible(true); } else{ JOptionPane.showMessageDialog(null,"Incorrect login or password","Error",JOptionPane.ERROR_MESSAGE); } } catch(Exception e){ System.out.println(e.getMessage()); } } }); ADD.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ CreateAccount account=new CreateAccount(); account.setVisible(true); } }); } public static void main(String arg[]) { Login l=new Login(); l.setBounds(90,150,200,30); l.setSize(300,100); l.setVisible(true); l.setTitle("LOGIN PAGE"); } } here is my start page:- import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class Start extends JFrame //implements KeyListener { JLabel title,title1,title2,line,image; JButton Login,Back; JPanel p; int key; String b; public Start() { setLayout(null); setTitle("Start"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); title=new JLabel("WELCOME TO AYURVEDA"); title.setFont(new Font("arial",Font.BOLD,50)); title.setForeground(Color.red); title.setBounds(200,50,1000,70); title1=new JLabel("LUNA PHARMA"); title1.setFont(new Font("arial",Font.BOLD,40)); title1.setForeground(Color.RED); title1.setBounds(350,100,500,70); title2=new JLabel("GMP CERTIFIED UNIT"); title2.setFont(new Font("arial",Font.BOLD,25)); title2.setForeground(Color.red); title2.setBounds(360,150,500,60); line=new JLabel("------------------"); line.setFont(new Font("arial",Font.BOLD,45)); line.setForeground(Color.blue); line.setBounds(360,200,1000,30); getContentPane().add(title); getContentPane().add(title1); getContentPane().add(title2); getContentPane().add(line); Login=new JButton("Login"); Login.setMnemonic(KeyEvent.VK_A); Login.setToolTipText("Press it to login"); Login.setFont(new Font("arial",Font.BOLD,20)); //Login.addActionListener(this); // Login.addKeyListener(this); getContentPane().add(Login); Back=new JButton("Exit"); Back.setMnemonic(KeyEvent.VK_B); Back.setToolTipText("Press it to Exit"); Back.setFont(new Font("arial",Font.BOLD,20)); // Back.addActionListener(this); // Back.addKeyListener(this); getContentPane().add(Back); p=new JPanel(); p.setLayout(new GridLayout(1,3)); p.setBounds(330,650,350,30); p.add(Login); p.add(Back); getContentPane().add(p); image=new JLabel(new ImageIcon("images/Front.jpg")); image.setBounds(100,250,900,500); getContentPane().add(image); setSize(1020,1000); setVisible(true); Back.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae){ { dispose(); } } }); Login.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { Login l=new Login(); } }); } /*public void keyPressed(KeyEvent ke) { key=ke.getKeyCode(); if(key==KeyEvent.VK_A) { Login l=new Login(); } if(key==KeyEvent.VK_B) { dispose(); } }*/ ///public void keyTyped(KeyEvent ke){} // public void keyReleased(KeyEvent ke){} public static void main(String args[]) { Start sf=new Start(); sf.setVisible(true); } }
Thanks a lot mugelan........! You are great in such a simple you have written a the above code.....! I understand it very easily.....and i successfully get through the first step of my project. god bless you..!
Ads