Problem with login screen

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pleb
    New Member
    • Dec 2007
    • 8

    #1

    Problem with login screen

    hi.. when i hit run all i get is the Clear button which fills the whole panel and nothing else comes up anyno where i went wrong

    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;

    public class Login extends JFrame implements ActionListener
    {
    JPanel cardHolder;
    private CardLayout card;
    private JLabel label1, label2;
    private JTextField username;
    private JPasswordField password;
    private JButton loginBtn, clearBtn, PurchaseTickets ;

    public static void main(String args[])
    {
    Login app = new Login();
    }

    public Login() //constructor
    {
    super("Event Handling");
    setTitle("Login ");

    cardHolder = new JPanel();
    card = new CardLayout();
    Container c = getContentPane( );
    cardHolder.setL ayout(card);

    label1 = new JLabel("Usernam e:");

    username = new JTextField(20);

    label2 = new JLabel("Passwor d:");

    password = new JPasswordField( 20);

    loginBtn = new JButton("Login" );
    loginBtn.addAct ionListener(thi s);

    clearBtn = new JButton("Clear" );
    clearBtn.addAct ionListener(thi s);

    c.add(label1);
    c.add(username) ;
    c.add(label2);
    c.add(password) ;
    c.add(loginBtn) ;
    c.add(clearBtn) ;

    setSize(350, 170);
    setVisible(true );
    }

    public void actionPerformed (ActionEvent e)
    {
    if (e.getSource() == loginBtn)
    {
    JOptionPane.sho wMessageDialog( null, "Welcome "+username.getT ext());
    select sel = new select();
    }

    if (e.getSource() == clearBtn)
    {
    username.setTex t("");
    password.setTex t("");
    }
    }
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    [QUOTE=pleb][code=java]
    c.add(label1);
    c.add(username) ;
    c.add(label2);
    c.add(password) ;
    c.add(loginBtn) ;
    c.add(clearBtn) ;
    [/code]

    You're adding all these component to your ContentPane which has a BorderLayout
    by default. All the components end up in the same area of the BorderLayout, and
    only the last one will be stored there and takes up the entire area available to
    the ContentPane.

    kind regards,

    Jos

    Comment

    Working...