如何从一个GUI类传递到另一个GUI类?

时间:2022-01-31 23:44:06

This is my LoginGUI class, I want to move from this GUI class to another "StudentGUI" class. It seems so simple but I can't figure out

这是我的LoginGUI类,我想从这个GUI类转移到另一个“StudentGUI”类。看起来很简单,但我无法弄清楚

    JButton btnNewButton_1 = new JButton("Log In");
    btnNewButton_1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {

            if(Username.equals(textField1.getText())){

                    if(Password.equals(textField2.getText())){
                        // close(LoginGUI);
                        // run(StudentGUI); 

                    **Missing function**

                        msg = "Loged in!";
                    }else{
                        msg = "Login Denied";
                    }
                }else{
                    msg = "Login Denied";
                }   
                JOptionPane.showMessageDialog(null,msg);                    
            }




        });
    btnNewButton_1.setBounds(157, 230, 89, 23);
    frame.getContentPane().add(btnNewButton_1);
}

}

1 个解决方案

#1


1  

If you would like to switch between multiple views, use the CardLayout Here's a simple example

如果您想在多个视图之间切换,请使用CardLayout这是一个简单的示例

package main.frames;

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

public class MainFrame extends JFrame
{
static JPanel homeContainer;
static JPanel homePanel;
static JPanel otherPanel;
static CardLayout cl;

public MainFrame()
{
    JButton showOtherPanelBtn = new JButton("Show Other Panel");
    JButton backToHomeBtn = new JButton("Show Home Panel");

    cl = new CardLayout(5, 5);
    homeContainer = new JPanel(cl);
    homeContainer.setBackground(Color.black);

    homePanel = new JPanel();
    homePanel.setBackground(Color.blue);
    homePanel.add(showOtherPanelBtn);

    homeContainer.add(homePanel, "Home");

    otherPanel = new JPanel();
    otherPanel.setBackground(Color.green);
    otherPanel.add(backToHomeBtn);

    homeContainer.add(otherPanel, "Other Panel");

    showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel"));
    backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home"));

    add(homeContainer);
    cl.show(homeContainer, "Home");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setTitle("CardLayout Example");
    pack();
    setVisible(true);
}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(MainFrame::new);
}
}

#1


1  

If you would like to switch between multiple views, use the CardLayout Here's a simple example

如果您想在多个视图之间切换,请使用CardLayout这是一个简单的示例

package main.frames;

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

public class MainFrame extends JFrame
{
static JPanel homeContainer;
static JPanel homePanel;
static JPanel otherPanel;
static CardLayout cl;

public MainFrame()
{
    JButton showOtherPanelBtn = new JButton("Show Other Panel");
    JButton backToHomeBtn = new JButton("Show Home Panel");

    cl = new CardLayout(5, 5);
    homeContainer = new JPanel(cl);
    homeContainer.setBackground(Color.black);

    homePanel = new JPanel();
    homePanel.setBackground(Color.blue);
    homePanel.add(showOtherPanelBtn);

    homeContainer.add(homePanel, "Home");

    otherPanel = new JPanel();
    otherPanel.setBackground(Color.green);
    otherPanel.add(backToHomeBtn);

    homeContainer.add(otherPanel, "Other Panel");

    showOtherPanelBtn.addActionListener(e -> cl.show(homeContainer, "Other Panel"));
    backToHomeBtn.addActionListener(e -> cl.show(homeContainer, "Home"));

    add(homeContainer);
    cl.show(homeContainer, "Home");
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setLocationRelativeTo(null);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    setTitle("CardLayout Example");
    pack();
    setVisible(true);
}

public static void main(String[] args)
{
    SwingUtilities.invokeLater(MainFrame::new);
}
}