【问题标题】:JPanels and handling events between themJPanels 和处理它们之间的事件
【发布时间】:2014-01-16 19:03:56
【问题描述】:

我正在创建一个具有以下面板的 GUI:

  1. 包含程序中所有面板的主框架:

    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();
    }
    

    }

  2. 登录面板 - 这是您打开程序时遇到的屏幕。

  3. 注册面板 - 按登录面板中的“注册”按钮时,您会进入此屏幕。

  4. 用户屏幕 - 显示用户信息的屏幕。

我遇到了一个不知道如何处理的问题:

单击登录面板中的“登录”按钮后,我想切换到新用户的屏幕(UserScreen 对象)。 由于类 Login 无法访问 UserScreen 对象(只有 Main 框架可以),我不得不从 Main 类设置按钮的 actionListener,这似乎是一个糟糕的解决方案(因为它需要我让 Main 访问许多登录面板中的字段,以便它可以检查具有给定用户名和密码的用户是否存在等)。有没有更好的办法?

有没有比我现有的更好的解决方案?

【问题讨论】:

  • 你可以实现一个Mediator
  • "JPanels 和处理它们之间的事件" 不要扩展 JPanel 而只是使用一个实例。一旦你弄清楚了,你就会意识到这不是问题。
  • 我试图找到类似的东西看看这个内容http://*.com/questions/10781401/switching-between-screens-in-java-swing.看看这是否对你有帮助任何方式

标签: java swing


【解决方案1】:

您应该实现模型-视图-控制器模式,不同视图之间的交互应该始终通过控制器。

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

【讨论】:

  • 你能详细说明一下吗?
  • @Shookie 看看this
  • 登录屏幕(视图)告诉控制器登录信息,控制器进行身份验证,关闭登录(视图),并使用用户信息(模型)使主框架(视图)正常运行。跨度>