我有一个带有CardLayout的表格。在这种布局中,我有很多纸牌。一切正常,直到我不得不使用按钮xclixck创建一种“移至上一个xcarsd”的方法。 CardLayout的默认next()back()没有用,因为我不知道用户将如何着陆特定的卡。

因此,我尝试管理要导航的卡片列表,并在单击按钮时将其移回列表。下面是我的实现。

  private List<String> goBackListHolder = new ArrayList<String>();
    private int firstPage = 0;

//Listeners
    private class AddNewClientsBtnAction extends MouseAdapter
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            CardLayout card = (CardLayout)mainPanelHolder.getLayout();
            card.show(mainPanelHolder, "new_client_form");
            goBackListHolder.add("new_client_form");
            firstPage++;
        }
    }

    private class AddProviderBtnAction extends MouseAdapter
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            CardLayout card = (CardLayout)mainPanelHolder.getLayout();
            card.show(mainPanelHolder, "new_provider_form");
            goBackListHolder.add("new_provider_form");
            firstPage++;
        }
    }

    private class AddIntroducerBtnAction extends MouseAdapter
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            CardLayout card = (CardLayout)mainPanelHolder.getLayout();
            card.show(mainPanelHolder, "new_introducer_form");
            goBackListHolder.add("new_introducer_form");
            firstPage++;
        }
    }

    private class AddIntroducerBtnAction2 extends MouseAdapter
    {
        @Override
        public void mouseClicked(MouseEvent e)
        {
            CardLayout card = (CardLayout)mainPanelHolder.getLayout();
            card.show(mainPanelHolder, "new_introducer_form");
            goBackListHolder.add("new_introducer_form");
            firstPage++;
        }
    }


// The listener of the Back Button

    private class GoBackBtnAction extends MouseAdapter
        {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                if(firstPage==0){}
                else
                {
                CardLayout card = (CardLayout)mainPanelHolder.getLayout();
                firstPage--;
                card.show(mainPanelHolder, goBackListHolder.get(firstPage));

                }
            }
        }


此方法有效,但有时会中断,然后移至错误的位置。上面的代码不是完整的代码,因为有很多侦听器。

那么,关于如何解决此问题的任何想法?

最佳答案

返回卡片时,还必须从列表末尾删除刚刚使用的卡片。

假设您使用了新的客户表格:

+---------------+
|new_client_form|
+---------------+
firstPage == 1 <- this is already wrong. List is indexed at 0.


然后转到新的提供者表格:

+---------------+   +-----------------+
|new_client_form|-->|new_provider_form|
+---------------+   +-----------------+
firstPage == 2


现在,如果您返回:

+---------------+   +-----------------+
|new_client_form|-->|new_provider_form|
+---------------+   +-----------------+
firstPage == 1


并再次转发:

+---------------+   +-----------------+   +-------------------+
|new_client_form|-->|new_provider_form|-->|new_introducer_form|
+---------------+   +-----------------+   +-------------------+
firstPage == 2


您可以看到firstPage即将结束。

我建议使用Stack而不是列表。然后,前进时可以push(),前进时可以pop()pop()将从栈顶元素中取出并提供给您,因此您不必跟踪索引。但是,请确保在离开该页面之前不要将卡推入堆栈。否则,如果您在推入卡时将其推入,则尝试回去时会得到同一张卡,并且您将被卡在自己的位置。

这是带有Stack的另一个示例:

转到新的客户表格:

//Empty stack


转到新的提供者表格:

+-----------------+
| new_client_form |
+-----------------+


转到新的介绍人表格:

+-------------------+
| new_provider_form |
+-------------------+
|  new_client_form  |
+-------------------+


再次转到新的客户表格:

+---------------------+
| new_introducer_form |
+---------------------+
|  new_provider_form  |
+---------------------+
|   new_client_form   |
+---------------------+


回去:

+-------------------+
| new_provider_form |
+-------------------+
|  new_client_form  |
+-------------------+
//Here you get the new_introducer_form passed back


再次返回:

+-----------------+
| new_client_form |
+-----------------+
// Here you get the new_provider_form passed back.


再次返回:

//Empty stack.
//Here you get the new_client_form passed back.

09-27 08:33