简体   繁体   中英

passing values from JPanel class to JFrame class

I have a JFrame which uses JPanel initialized from the JPanel.

This is the JFrame class : LoginPage

public class LoginPage extends JFrame
{
private JPanel contentPane;
static int cnf;
static String data;
private static LoginPage frame;

/**
 * Launch the application.
 */
public static void main(String[] args)
{
    EventQueue.invokeLater(new Runnable()
    {
        public void run()
        {
            try
            {
                frame = new LoginPage();
                frame.setVisible(true); 
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    });

    //cnf = chk;
    if( cnf == 1)
    {
        frame.dispose();
        JFrame m = new MainPage();
        m.setVisible(true);
    }
}

/**
 * Create the frame.
 */
public LoginPage()
{
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JPanel m = new MainLogin();
    m.setBounds(0, 0, 448, 271);
    contentPane.add(m);
}
}

And, this is the JPanel class : MainLogin

public class MainLogin extends JPanel
{
private JTextField uname;
private JPasswordField pass;
public static int chk;

public MainLogin()
{
    setLayout(null);

    uname = new JTextField();
    uname.setBounds(236, 22, 167, 25);
    add(uname);
    uname.setColumns(10);

    pass = new JPasswordField();
    pass.setBounds(236, 53, 167, 25);
    add(pass);

    JButton login = new JButton("Login");
    login.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            String u = uname.getText();
            char[] tp = pass.getPassword();
            String p = new String(tp);

            chk = authentication.verify(u, p);
            System.out.println(chk);
        }
    });
    login.setBounds(235, 90, 117, 25);
    add(login);
}
}

As in the MainLogin Panel, there is a class authentication who has a method verify() , which returns an integer, and this integer is stored in chk . Since, this chk resides in MainLogin JPanel class, I want to pass it to the LoginPage JFrame class. Is there any way to do this other than using a File?

Open the main page from LoginPage instance not from the main method.

Add a login() method to the LoginPage

public class LoginPage extends JFrame {

    //other parts

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    frame = new LoginPage();
                    frame.setVisible(true); 
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

    public LoginPage() {
        //...
        JPanel m = new MainLogin(this);
        //...
    }

    public void login(int chk) {
        JFrame m = new MainPage();
        m.setVisible(true);
        this.dispose();
    }
}

And pass login frame to the panel as a parameter

public class MainLogin extends JPanel
{

private int chk;//no need to be static

public MainLogin(final LoginFrame loginFrame)
{
    setLayout(null);//null layout is bad

    //...
    login.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            //...
            chk = authentication.verify(u, p);
            loginFrame.login(chk);
        }
    });
    //...
}
}

Not only does this question get asked quite a bit, it also has a number of possible solutions, including using a modal dialog or Observer Pattern depending on your needs

See How to Make Dialogs for more details

You might also like to take a look at

for more discussion on the subject

The basic answer here is, you want to separate the areas of responsibility.

You need to:

  • Gather the user credentials
  • Validate those credentials
  • Take a appropriate action based on the success of that check

These are three distinct actions, all which should be separated, it's not the responsibility of the login panel to validate the credentials, that's someone else's responsibility, equally, it's not the validators responsibility to decide what should be done when the validation fails or succeeds, that's someone else's responsibility

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