Java JPanel和处理它们之间的事件

Java JPanel和处理它们之间的事件,java,swing,Java,Swing,我正在创建一个包含以下面板的GUI: 容纳程序中所有面板的主框架: public class Main extends JFrame { private Signup signupForm; private Login login; private UserScreen user; private JPanel cards; private CardLayout cl; private static final int INNER_FRAME

我正在创建一个包含以下面板的GUI:

  • 容纳程序中所有面板的主框架:

    public class Main extends JFrame {
        private Signup signupForm;
        private Login login;
        private UserScreen user;
        private JPanel cards;
        private CardLayout cl;  
    
        private static final int INNER_FRAME_WIDTH=800;
        private static final int INNER_FRAME_HEIGHT=800;
    
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public Main() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, INNER_FRAME_HEIGHT, INNER_FRAME_WIDTH);
        getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
    
        cards = new JPanel();
        getContentPane().add(cards);
    
        cl = new CardLayout();
        cards.setLayout(cl);
    
        login = new Login();
        cards.add(login,"login");
    
    
        signupForm = new Signup();
        cards.add(signupForm,"signup");
    
        ActionListener listen = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            try{
                //check for a user with the parameters from userName and password fields . On success create a UserScreen for the current user.
                //This means that you need to send the User object to the UserScreen c'tor. It should look something like this. Might need to change UserScreen accordingly.
                user = new UserScreen();
                cards.add(user,"user");
                cl.show(cards,"user");
            }
            catch (Exception exception){ //TODO: Change to exception thrown from Client constructor
                //TODO: Have the exception popup the relevant screen.
            }
        }       
    
    };
    
        login.loginBtn.addActionListener(listen); << Example of setting a button's actionListener from Main.
    
        setLoginListeners();
        setSignupListeners();
    }
    
    public类主框架{
    私人注册表格;
    私人登录;
    私人用户屏幕用户;
    私人JPanel卡;
    私人信用卡;
    私有静态最终内部帧宽度=800;
    专用静态最终内部框架高度=800;
    /**
    *启动应用程序。
    */
    公共静态void main(字符串[]args){
    invokeLater(新的Runnable(){
    公开募捐{
    试一试{
    主机架=新主机架();
    frame.setVisible(true);
    }捕获(例外e){
    e、 printStackTrace();
    }
    }
    });
    }
    /**
    *创建框架。
    */
    公用干管(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    立根(100,100,内框高度,内框宽度);
    getContentPane().setLayout(新的GridLayout(0,1,0,0));
    卡片=新的JPanel();
    getContentPane()。添加(卡片);
    cl=新的CardLayout();
    卡片.设置布局(cl);
    login=新登录();
    添加(登录,“登录”);
    signupForm=新注册();
    卡片。添加(注册表格,“注册”);
    ActionListener Listener=新建ActionListener(){
    @凌驾
    已执行的公共无效操作(操作事件e){
    试一试{
    //使用用户名和密码字段中的参数检查用户。成功后,为当前用户创建用户屏幕。
    //这意味着您需要将用户对象发送到UserScreen c'tor。它应该是这样的。可能需要相应地更改UserScreen。
    user=newuserscreen();
    添加(用户,“用户”);
    cl.show(卡片,“用户”);
    }
    catch(异常){//TODO:更改为从客户端构造函数引发的异常
    //TODO:将异常弹出到相关屏幕。
    }
    }       
    };
    
    login.loginBtn.addActionListener(listen);您应该实现模型-视图-控制器模式,不同视图之间的交互应该始终通过一个控制器


    您可以实现一个
    中介
    “jpanel并处理它们之间的事件”不要扩展
    JPanel
    ,只需使用一个实例即可。一旦你明白了这一点,你就会意识到这不是一个问题。我正试图找到类似的东西,看看这个公司ntenthttp://stackoverflow.com/questions/10781401/switching-between-screens-in-java-swing.see 如果这对你有帮助,你能详细说明一下吗?@Shookie查看登录屏幕(视图)告诉de控制器登录信息,控制器进行身份验证,关闭登录(视图),并使用用户信息(模型)调整主框架(视图)。