Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Need some help with this JFrame, or JDialog.

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need some help with this JFrame, or JDialog.

    well, bascially i am making a little log-in system for myself.
    i created a JFrame in which you create an account, when you click "ok" it pops up a window with the information you just filled in(password and username).
    then if you click "OK" on that window too, i want another JFrame to pop up that actually lets you log in with the information.

    how can i create 2 JFrames in a single class?

    this is the code:
    import javax.swing.JOptionPane;
    import java.awt.event.ActionListener;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    import javax.swing.JPasswordField;
    import javax.swing.JPanel;
    public class LogIn extends JFrame{
     
    	private JTextField txt1;
    	private JTextField txt2;
    	private JPasswordField ps1;
    	private JPasswordField ps2;
    	private JPasswordField ps3;
    	private JLabel label1;
    	private JLabel label2;
    	private JLabel label3;
    	private JLabel label4;
    	private JLabel label5;
    	private JButton b1;
    	private JButton b2;
     
     
     
     
     
    public LogIn(){
    	super("Account");
    	setLayout (new FlowLayout());
    	label1 = new JLabel("Enter your username: ");
    	txt1 = new JTextField(10);
    	label2 = new JLabel("Enter your password: ");
    	ps1 = new JPasswordField(10);
    	label3 = new JLabel("Confirm your password: ");
    	ps2 = new JPasswordField(10);
    	b1 = new JButton("OK");
    	add(label1);
    	add(txt1);
    	add(label2);
    	add(ps1);
    	add(label3);
    	add(ps2);
    	add(b1);
     
     
    	thehandler handler = new thehandler();
    	b1.addActionListener(handler);
     
     
    }
    public class thehandler implements ActionListener{
    	public void actionPerformed(ActionEvent event){
    		if(txt1.getText().equals(ps1.getText())){
    		JOptionPane.showMessageDialog(null, "Password error, your username can't be the same as your password","Password Error!",JOptionPane.ERROR_MESSAGE);}
    		else if(ps1.getText().equals(ps2.getText())){
    		JOptionPane.showMessageDialog(null, "Account information: "+"\n"+"User: "+txt1.getText()+"\n"+"Password: "+ps1.getText(), "Account Information", JOptionPane.INFORMATION_MESSAGE);}
    		else{
    		JOptionPane.showMessageDialog(null, "Passwords did not match! Try again please!", "Password error!", JOptionPane.ERROR_MESSAGE);}
     
     
     
     
     
     
     
     
    	}
     
     
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with this JFrame, or JDialog.

    how can i create 2 JFrames in a single class?
    Use the new statement:
    frame1 = new JFrame();
    frame2 = new JFrame(); //  Create second JFrame
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with this JFrame, or JDialog.

    but im not sure how to actually create and set up a JFrame like that.

    i created my first JFrame like this:
    public LogIn(){
    	super("Account");
    	setLayout (new FlowLayout());
    	label1 = new JLabel("Enter your username: ");
    	txt1 = new JTextField(10);
    	label2 = new JLabel("Enter your password: ");
    	ps1 = new JPasswordField(10);
    	label3 = new JLabel("Confirm your password: ");
    	ps2 = new JPasswordField(10);
    	b1 = new JButton("OK");
    	add(label1);
    	add(txt1);
    	add(label2);
    	add(ps1);
    	add(label3);
    	add(ps2);
    	add(b1);

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with this JFrame, or JDialog.

    i created my first JFrame like this
    That code does NOT create a JFrame. It looks like a constuctor for a class that was created somewhere else.
    A class instance/object is created by a new statement. Somewhere there is a call to new Login() that creates an instance of the Login class.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with this JFrame, or JDialog.

    oh yeah indeed, i have that class with setSize etc. etc.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with this JFrame, or JDialog.

    The new statement is used to create an instance of a class.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    May 2013
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need some help with this JFrame, or JDialog.

    can you give me an example of how to create a second JFrame in one class?
    cause i didnt quite understand what you just said

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need some help with this JFrame, or JDialog.

    how to create a second JFrame in one class?
    I described that in post #2:
    JFrame frame2 = new JFrame(); //  Create second JFrame

    After the instance of JFrame is created, the code needs to add components to it and call the setVisible() method like what is done in the LogIn class's constructor code in post#3
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JDialog not working properly
    By agarta in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 24th, 2013, 10:46 PM
  2. different between japplet, jframe, jdialog, jwindow, jpanel
    By hwoarang69 in forum AWT / Java Swing
    Replies: 1
    Last Post: October 27th, 2012, 10:53 PM
  3. Replies: 1
    Last Post: April 30th, 2012, 08:16 AM
  4. JDialog Failing to Render...
    By snowguy13 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 7th, 2012, 11:55 AM
  5. JDialog from JFrame button (GUI)
    By Jameseyboy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2012, 07:55 AM