简体   繁体   中英

JDialog login with JFrame

I am trying to make a 'modal' JDialog ( I hope this is what it's supposed to look like), I want to enter my user and password into my dialog and if it's correct then open my main JFrame where I parse some info. My problem occurs when I do the login button, i cannot refer to the current object(this) with it's textboxes, etc; This is the code, and here's the error :

图像错误

Also I'm a bit confuse because when I'll have a successful login, I am supposed to open a JFrame..... not sure how to proceed because I am supposed to work with only one JFrame and switch my panels. Thanks in advance

package test_area;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;




public class jpanel1 extends JFrame{

    static boolean loginBool = false; //if this goes true, then I go to my frame




    // JPANEL---------------------------------------------
    public static class panel1 extends JPanel implements ActionListener{


        //simple constructor
    public panel1(){

        JPanel a = this;

        a.setLayout(new GridLayout(5,1,5,5));  

        JLabel username = new JLabel("Username");
        JTextField usernameTxt = new JTextField(8);
        JLabel password = new JLabel("Password");
        JPasswordField passwordTxt = new JPasswordField(55);
        JButton doIt = new JButton("Log In");

        doIt.addActionListener(this);

        a.add(username);
        a.add(usernameTxt);
        a.add(password);
        a.add(passwordTxt);
        a.add(doIt);


        a.setSize(200,200);
        a.setVisible(true);


    }

        //constructor with param
  //  public panel1(JLabel a, JTextField b, JLabel c, JPasswordField d, JButton e){

    //}


    //@Overwritten method

        @Override
        public  void actionPerformed(ActionEvent ae) {
            String user;
            String pw;

            user = a.usernameTxt.getText();
            pw   = a.passwordTxt.getText();

            // package does not exist
        }

}
    // JPANEL---------------------------------------------




    //JFrame
    public jpanel1(){

        JFrame frame = new JFrame("Login Pane");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(new panel1());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

    }


    public static void main(String[] args)
    {
       jpanel1 gg = new jpanel1(); //frame;


    }

}

Declared a static boolean that is supposed to kick in once the login is correct, then somehow open the new JFrame...

Your code doesn't even have a reference to the JDialog...

Assuming frame is your application's main window of type JFrame and dialog your login dialog of type JDialog . At the begin show the modal login dialog.

frame.setVisible(true);
dialog.setModal(true);
dialog.setVisible(true);

When login succeeds hide the dialog with

dialog.setVisible(false);

You don't need to change modality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM