从JPanel返回信息

时间:2014-01-25 18:29:43

标签: java user-interface jframe jpanel

我有一个JFramePanel 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);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这取决于谁在传递这些信息方面具有“积极”作用。 RegistrationPanel是否应该将信息传递回StartFrame(例如,单击RegsitrationPanel上的按钮?)或者StartParame 从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();
        ...
    }