我有一个JFrame,其中的Panel用于JButtons在两个JPanelsRegistrationPanelConfirmationPanel)之间导航。使用JPanels可以在两个CardPanel/CardLayout之间相互切换。当用户在RegistrationPanel中输入信息时,是否有办法将这些信息返回到下面列出的StartFrame

import java.awt.BorderLayout;

public class StartFrame extends JFrame implements ActionListener
{
    JButton nextButton = new JButton("Next");
    JButton previousButton = new JButton("Previous");
    JButton registerButton = new JButton("Register");
    JPanel cardPanel = new JPanel();
    CardLayout cardLayout = new CardLayout(10, 10);

    public StartFrame()
    {
        setLayout(new BorderLayout(5, 5));
        nextButton.addActionListener(this);
        previousButton.addActionListener(this);
        previousButton.setVisible(false);
        registerButton.addActionListener(this);
        registerButton.setVisible(false);

        Panel buttonsPanel = new Panel(new FlowLayout(FlowLayout.CENTER));
        buttonsPanel.add(previousButton);
        buttonsPanel.add(nextButton);
        buttonsPanel.add(registerButton);

        setCardLayout();
        add(BorderLayout.SOUTH, buttonsPanel);
        add(BorderLayout.CENTER, cardPanel);
    }

    private void setCardLayout()
    {
        cardPanel.setLayout(cardLayout);
        cardPanel.add(new RegistrationPanel(), "registration");
        cardPanel.add(new ConfirmationPanel(), "confirmation");
        cardLayout.show(cardPanel, "registration");
    }

    @Override
    public void actionPerformed(ActionEvent event)
    {
        if (event.getSource().equals(nextButton))
        {
            cardLayout.next(cardPanel);
            previousButton.setVisible(true);
            registerButton.setVisible(true);
            nextButton.setVisible(false);
        }

        else
        {
            cardLayout.previous(cardPanel);
            previousButton.setVisible(false);
            registerButton.setVisible(false);
            nextButton.setVisible(true);
        }
    }
}

最佳答案

这取决于谁在传递此信息时扮演“主动”角色。 RegistrationPanel是否应该将信息传递回StartFrame(例如,当单击RegsitrationPanel上的按钮时?)还是StartFrame应该从RegistrationPanel获取信息(例如,当单击StartFrame上的按钮时)?

在第一种情况下,RegistrationPanel需要对StartFrame的引用(最好以某种更“抽象”的形式,以避免对这个具体类的依赖)。在第二种情况下,StartFrame需要对RegistrationPanel的引用。

粗略地勾勒出这里的方法:

对于第一种情况:

class RegistrationPanel
{
    private StartFrame startFrame;
    public RegistrationPanel(StartFrame startFrame)
    {
        this.startFrame = startFrame;
    }

    // Called e.g. when a button was clicked on this panel
    void passBackInformationToStartFrame()
    {
        startFrame.setInfo(this.getInfo());
    }
}


class StartFrame ...
{
    ...
    private void setCardLayout()
    {
        cardPanel.setLayout(cardLayout);

        // Remove:
        //cardPanel.add(new RegistrationPanel(), "registration");

        // Add:
        cardPanel.add(new RegistrationPanel(this), "registration");

        ....
    }
}


对于第二种情况

public class StartFrame extends JFrame implements ActionListener
{
    ...
    private RegistrationPanel registrationPanel

    private void setCardLayout()
    {
        cardPanel.setLayout(cardLayout);

        // Remove:
        //cardPanel.add(new RegistrationPanel(), "registration");

        // Add:
        registrationPanel = new RegistrationPanel();
        cardPanel.add(registrationPanel, "registration");
    }

    // Called e.g. when a button is clicked on this frame:
    void fetchInformationFromRegistrationPanel()
    {
        Info info = registrationPanel.getInfo();
        ...
    }

10-08 01:37