【问题标题】:JAVA cardLayout. how to call a card from different classesJAVA卡片布局。如何调用不同类别的卡
【发布时间】:2017-12-14 07:44:17
【问题描述】:

我有一个带有框架的程序,其中包含一个带有卡片布局布局的主面板,我希望它显示不同的卡片/面板。

在我的情况下,我真的很难从按钮动作侦听器中调用新卡。

我希望在单击按钮后出现一张新卡片,但我在动作侦听器中输入的代码都没有显示我想要的卡片。

我知道我的 actionListener 工作,因为我在里面做了一个 println。

这是我的代码。我去掉了任何不必要的东西,这样更容易阅读。感谢您的帮助!

我会接受所有关于代码结构的建议

主框架:

public class MainFrame extends JFrame{

final static String CONNEXION_VIEW = "connexionView"; 
final static String CONNEXION_FAILED_VIEW = "connexionRefusee"; 

public MainFrame()
{
    super();
    initialize();
}


private void initialize()
{
    getMainPanel();

    add(getMainPanel());

}

CardLayout cardLayout;
public CardLayout getCardLayout()
{
    if (cardLayout == null)
    {
        cardLayout = new CardLayout();
    }
    return cardLayout;
}


JPanel mainPanel;
public JPanel getMainPanel()
{
    if (mainPanel == null)
    {
        mainPanel = new JPanel();

        mainPanel.setLayout(getCardLayout());

        mainPanel.add(CONNEXION_VIEW, getConnexionView());
        mainPanel.add(CONNEXION_FAILED_VIEW, getConnexionFailedView());

    }
    return mainPanel;
}

ConnexionView connexionView;
protected ConnexionView getConnexionView()
{
    if (connexionView == null)
    {
        connexionView = new ConnexionView();
    }
    return connexionView;
}

ConnexionFailedView connexionFailedView;
protected ConnexionFailedView getConnexionFailedView()
{
    if (connexionFailedView == null)
    {
        connexionFailedView = new ConnexionFailedView();
    }
    return connexionFailedView;
}

connexion 视图,带有按钮的视图,通过操作侦听器单击我想要放置我的代码的位置

public class ConnexionView extends JPanel{

GridBagLayout gbl = new GridBagLayout();

private JButton btnConnexion;

Dimension dimensionBouton = new Dimension(170, 30);

public ConnexionView()
{
    super();
    initialise();
}

private void initialise()
{
    setLayout(gbl);

    GridBagConstraints gbcbtnConnexion = new GridBagConstraints();
    gbcbtnConnexion.gridwidth = GridBagConstraints.REMAINDER;
    gbcbtnConnexion.gridheight = GridBagConstraints.REMAINDER;
    gbcbtnConnexion.gridx = 1;
    gbcbtnConnexion.gridy = 2;
    add(getBtnConnexion(), gbcbtnConnexion);
}

private JButton getBtnConnexion()
{
    if (btnConnexion == null)
    {
        btnConnexion = new JButton("Connexion");
        btnConnexion.setPreferredSize(dimensionBouton);
        btnConnexion.setMinimumSize(dimensionBouton);

        btnConnexion.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                /////code to display the connexion_Failed_View

                System.out.println("test");
            }
        });
    }
    return btnConnexion;
}

}

和连接失败视图,我想在单击按钮后显示的视图

public class ConnexionFailedView extends JPanel{

public ConnexionFailedView()
{
    super();
    initialise();
}

private void initialise()
{
    setBackground(Color.YELLOW);
}

提前致谢

【问题讨论】:

    标签: java swing layout-manager cardlayout


    【解决方案1】:

    您需要以某种方式将组件保留在您的 Button 中,以便您可以访问它。

    class ConnexionView {
        private JComponent mainPanel;
        public ConnexionView(JComponent mp) { mainPanel = mp; }
    }
    

    显然,这意味着 MainFrame 需要通过它。

    现在,听者可以做

    // it would be cleaner if you passed the layout in the constructor as well
    CardLayout cl = (CardLayout) mainPanel.getLayoutManager();
    cl.show(mainPanel, MainFrame.CONNEXION_FAILED_VIEW);
    

    【讨论】:

    • @Clement 太好了。随意选择答案旁边的复选标记以将其标记为已回答:)
    猜你喜欢
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 2013-08-07
    • 2015-05-21
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多