-1

i have done coding for login and when clicked on Login Button, should navigate to JPanel ap from JPanel lp. Kindly help me if there is any mistakes or modifications in my code. I have checked many codes and from that I have edited maximum and i prefer simple step

import java.awt.*;
import javax.swing.*;
import javax.swing.border.LineBorder;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class LoginPage extends JFrame implements ActionListener
{   
   JLabel jl,jl2,jl3,jl4;
   JTextField jt,jt2;
   JTextArea jt1;
   JButton jb,jb2;
   JPanel lp, ap;

   JPasswordField pass,Passw;
    public static void main(String args[])
    {
      LoginPage jf=new LoginPage("Login");
      Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
      int a=d.height/3;
      int b=d.width/12;
      jf.setBounds(a,b,660,500);
      jf.setResizable(false);
      jf.setVisible(true);
      jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      jf.setLayout(null);
    }
    public LoginPage(String title)
    {
       //First Panel containing Login details
       lp=new JPanel(); 
       lp.setLayout(null);

       jl=new JLabel("Login to your Account Safely");
       jl.setBounds(250,30,300,30);
       lp.add(jl);

       jl2=new JLabel("UserName");
       jl2.setBounds(100,150,100,30);
       lp.add(jl2);

       jt=new JTextField(JTextField.CENTER);
       jt.setBounds(250,150,300,25);
       jt.setBorder(new LineBorder(Color.BLACK));
       lp.add(jt);

       jl3=new JLabel("Password");
       jl3.setBounds(100,250,100,30);
       lp.add(jl3);

       pass=new JPasswordField(10);
    pass.setBounds(250,250,300,25);
    pass.setBorder(new LineBorder(Color.BLACK));
    lp.add(pass);


    jb=new JButton("Login");
    jb.setBounds(250,350,80,25);
    jb.setBorder(new LineBorder(Color.BLUE));
    jb.setCursor(new Cursor(Cursor.HAND_CURSOR));
    jb.addActionListener(this);
    lp.add(jb);


    jb2=new JButton("Cancel");
    jb2.setBounds(450,350,80,25);
    jb2.setBorder(new LineBorder(Color.RED));
    jb2.setCursor(new Cursor(Cursor.HAND_CURSOR));
    jb2.addActionListener(this);
    lp.add(jb2);
    add(lp);

    // Next Panel containing Account details
    ap=new JPanel();
    ap.setLayout(null);

    jl4=new JLabel("Login to your Account Safely");
    jl4.setBounds(250,30,300,30);
    ap.add(jl4);

    jt2=new JTextField(JTextField.CENTER);
    jt2.setBounds(250,150,300,25);
    jt2.setBorder(new LineBorder(Color.BLACK));
    ap.add(jt2);

}

public void actionPerformed(ActionEvent ae)
{
    if(ae.getSource()==jb)
    {
        if(("".equals(jt.getText())) || ("".equals(pass.getText())))
        {
            JOptionPane.showMessageDialog(null,"Username and Password fields are mandatory");
        }
        else if(("admin".equals(jt.getText())) && ("admin".equals(pass.getText())))
        {   
            lp.setVisible(false);
            add(ap);

        }
        else 
        {
            JOptionPane.showMessageDialog(null,"Invalid Username or Password");
        }
    }
    else if(ae.getSource()==jb2)
    {
        System.exit(0);
    }
}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `jf.setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 25 '14 at 08:01
  • 1
    Use CardLayout. Place both your panel in a top panel wich has the card layout as layout manager and switch the visible panel in actionPerformed method. Here is the link to CardLayout howto: http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html – Sergiy Medvynskyy Sep 25 '14 at 08:01
  • This question appears to be off-topic because it is about matters well explained in [How to Use CardLayout](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). – Andrew Thompson Sep 25 '14 at 08:05

1 Answers1

1

This quesiton is probably what you are looking for :

How do I change JPanel inside a JFrame on the fly?

However I would use a CardLayout in your case. Adding/Removing panels from a frame is more complicated than it seems.

Community
  • 1
  • 1
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225