CardLayout:如何在另一个类的ActionListeners中显示一个类中的面板? [英] CardLayout: How to show panels in one class in the ActionListeners in another class?

查看:168
本文介绍了CardLayout:如何在另一个类的ActionListeners中显示一个类中的面板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JPanel中添加了buttons(具有border layout,将按钮添加到了south位置),然后在another JPanel cardsPanel的(center )父面板,并为其指定了CardLayout. 我希望此cardsPanel成为卡的container (我是指共享相同屏幕空间的面板).

I added my buttons in a JPanel (which has a border layout, added the buttons to its south position), then added another JPanel cardsPanel in the (center position of the) parent panel, and gave it a CardLayout. I want this cardsPanel to be the container for the cards (I mean the panels which will share the same screen space).

现在我需要做的是显示这些卡(我称这些面板将与卡共享相同的屏幕空间)作为对父面板中按钮的响应.但是这些按钮位于父面板中,它们的动作侦听器也显然也位于该面板中.

Now what I need to do is to to display those cards (I am calling the panels which will share the same screen space as cards) as a response to the buttons in the parent panel. But the buttons are located in the parent panel and their action listeners will obviously be located there too.

所以问题是,我如何show(由cardLayout.show())位于容器类(具有Cardlayout)中的面板作为对位于另一类(具有BorderLayout中的按钮)的响应)?

So the question is that how do I show (by cardLayout.show()) the panels located in a container class (with Cardlayout) as a response to buttons located in another class (with BorderLayout) ?

编辑-有关操作的问题:-

我希望发现实现ActionActionListener更有用. 我还读到它们可以重复使用,并用于执行相同功能的按钮.我有几个按钮可以执行翻转特定卡的功能(即在CardLayout中显示特定面板) ). 我只写了一个内部类,我想将其用于所有按钮.

I hope to find implementing an Action more useful than an ActionListener. I also read that they can be reused, and used for the buttons which perform the same function. I have a few buttons which perform the function of flipping a particular card (i.e. displaying a particular panel in the CardLayout). I have written just one inner class and I want to use it for all the buttons.

所以我想将按钮标识符(当我们将按钮添加到其父对象时用于标识按钮的字符串,例如String btnIdentifier= "1"; panel.add(button1, btnIdentifier);)传递给此类的方法actionPerformed(),尽管我们从未明确调用actionPerformed (). 因此,如何将按钮的字符串标识符传递给actionPerformed()方法,以便可以对所有按钮使用相同的Action.

So I will like to pass the button identifier (the string used to identify a button when we add the button to its parent e.g. String btnIdentifier= "1"; panel.add(button1, btnIdentifier); ) to the method actionPerformed() of this class, eventhough we are never explicitly calling actionPerformed(). So how can I pass the string identifier of the button to the actionPerformed() method so that I can use the same Action for all my buttons.

最小代码:-

class Action extends AbstractAction{
            Action(String text, ImageIcon icon){
                super(text, icon);

            }
            public void actionPerformed(ActionEvent e) {
                button1.setBorderPainted(true);
                button1.setContentAreaFilled(true);
                if (btnIdentifier=="1"){  //////ASSUMING THAT BUTTON IDENTIFIER HAS SOMEHOW BEEN PASSED TO THIS METHOD
                    FirstCard firstCard= new FirstCard();
                    cardsPanel.add(firstCard, btnIdentifier);
                    cardLayout.show(cardsPanel, btnIdentifier);
                } else if (btnIdentifier=="2"){  //////ASSUMING THAT BUTTON IDENTIFIER HAS SOMEHOW BEEN PASSED TO THIS METHOD
                    SecondCard secondCard= new SecondCard();
                    cardsPanel.add(secondCard, btnIdentifier);
                    cardLayout.show(cardsPanel, btnIdentifier);
                } else if (btnIdentifier=="3"){  //////ASSUMING THAT BUTTON IDENTIFIER HAS SOMEHOW BEEN PASSED TO THIS METHOD
                    ThirdCard thirdCard= new ThirdCard();
                    cardsPanel.add(thirdCard, btnIdentifier);
                    cardLayout.show(cardsPanel, btnIdentifier);
                } 
            }
        }
        }

推荐答案

使用此处 Action > package-private 访问权限,如此处所示.

Use Action to encapsulate the desired functionality. Make a reference to the enclosing panel visible, as shown here and here; or export the Action from a class having (at least) package-private access, as shown here.

这篇关于CardLayout:如何在另一个类的ActionListeners中显示一个类中的面板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆