繁体   English   中英

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

[英]How to pass from one GUI class to another GUI class?

这是我的 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);
}

}

如果你想在多个视图之间切换,使用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);
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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