简体   繁体   English

从子 JPanel 返回一个值给父 JFrame

[英]Return a value from the child JPanel to the parent JFrame

I have a MainFrame class where I hold a JFrame and open a new JPanel as LoginPanel .我有一个MainFrame class ,其中我持有一个JFrame并打开一个新的JPanel作为LoginPanel In this LoginPanel class I have a login button and an ActionListener .在这个LoginPanel class 我有一个登录按钮和一个ActionListener The action listener should check the login credentials and then return to the MainFrame class any value maybe a boolean so I can open a new JPanel for the corresponding user:操作侦听器应检查登录凭据,然后返回到MainFrame class 任何值可能是boolean以便我可以为相应用户打开一个新的JPanel

public class MainFrame extends JFrame {
...
this.loginPanel = new LoginPanel();
this.add(loginPanel);

--> Here I would like to open a new JPanel and open a new JPanel 
    for the logged in user based on the value returned by the LoginPanel

...
}
public class LoginPanel extends JPanel{
...
// Create the action for the login button
   loginButton.addActionListener(e -> {
       // TODO: check if the given username is available
       // TODO: return maybe a boolean so we know that the user logged in succesfully
   });

I would be very thankful for any Idea on how to change my software design to implement this logic.我将非常感谢有关如何更改我的软件设计以实现此逻辑的任何想法。 Cheers!干杯!

Your question involves multiple steps, I'm afraid, but the bottom line is how to get one object to communicate with another, and the standard way to do this is by calling public methods of one into the other.恐怕您的问题涉及多个步骤,但底线是如何让一个 object 与另一个进行通信,而做到这一点的标准方法是将一个公共方法调用到另一个。 One simple way is to give your LoginPanel class a MainFrame field, one that is filled via a constructor:一种简单的方法是为您的 LoginPanel class 提供一个 MainFrame 字段,该字段通过构造函数填充:

public class LoginPanel {
    private MainFrame mainFrame;
    private JButton loginButton = ....;
    
    public LoginPanel(MainFrame mainFrame) {
        this.mainFrame = mainFrame;
        
        loginButton.addActionListener(e -> {
            // extract the key properties from this class
            // such as text in a JTextField and the char[] in a password field
            // test it for appropriateness, and then send the information 
            // to the mainFrame object by calling a method
            
            // e.g.,
            // mainFrame.userVerified(/* boolean test in here */);
            
            // e.g.,
            mainFrame.userVerified(
                    userNameField.getText().equals(someUserNameVariable)
                    && Arrays.equals(passwordField.getPassword(), someCharArrayVariable));
        });
    }

So MainFrame would need to pass itself into LoginPanel:所以 MainFrame 需要将自己传递给 LoginPanel:

public class MainFrame extends JFrame {
    public MainFrame() {
        // ...
        loginPanel = new LoginPanel(this);
        add(loginPanel);
        // ...
    }
    
    public void userVerified(boolean verified) {
        // act on it, swapping JPanels using a CardLayout
    }

A better solution, one that "scales well" is to separate concerns, the GUI from the non-GUI model and use a Model-View-Controller or similar type structure, but for your purposes, that may be over-kill at this point.一个更好的解决方案,一个“扩展性很好”的解决方案是将关注点从非 GUI model 中分离出来,并使用 Model-View-Controller 或类似类型的结构,但出于您的目的,此时可能会过度杀戮。

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

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