使用 CardLayout 将自定义面板添加到 Applet
我无法弄清楚 CardLayout 的 JavaDocs。我有一个 Applet,从这个 Applet 中我创建了 5 个扩展 JPanel 的类。在这些类中,到目前为止所做的一切都是设计(一些 GUI 组件)。现在我想通过 Applet 将所有这些类链接在一起,以便一次查看一个面板(CardLayout)。因此,我将能够从 Applet 中使用 CardLayout 的 next 方法来查看下一个面板。这是我的代码:
setLayout(new CardLayout());
add(mainView); //mainView, managerView, searchView, storesView and hoursView
add(managerView); // are initialized
add(searchView);
add(storesView);
add(hoursView);
这是我的事件处理代码:
public void actionPerformed(ActionEvent e)
{
CardLayout cl; //CardLayout object to manipulate the next page
cl = (CardLayout)(this.getLayout());
if(e.getSource() == mainView.getManagerButton())
{
cl.next(this);
}
if(e.getSource() == mainView.getSearchButton())
{
cl.next(this); //if the user hits the searchButton I want to link to panel
cl.next(this); // searchView. Is that correct?
}
}
使用此代码,我得到一个 IllegalArgumentException
有人请指出我的错误!我还针对代码中的问题提供了一些评论。 一如既往,谢谢!
I cannot figure out the JavaDocs for the CardLayout. I have an Applet, and from this Applet I have 5 classes I created that extend JPanel. Inside these classes all that has been done so far is the design (some GUI components). Now I want to link all these classes together through the Applet so one panel is viewed at a time (CardLayout). Thus, I will have the capability from my Applet to use CardLayout's next method to view the next panel. Here is my code:
setLayout(new CardLayout());
add(mainView); //mainView, managerView, searchView, storesView and hoursView
add(managerView); // are initialized
add(searchView);
add(storesView);
add(hoursView);
Here is my event handling code:
public void actionPerformed(ActionEvent e)
{
CardLayout cl; //CardLayout object to manipulate the next page
cl = (CardLayout)(this.getLayout());
if(e.getSource() == mainView.getManagerButton())
{
cl.next(this);
}
if(e.getSource() == mainView.getSearchButton())
{
cl.next(this); //if the user hits the searchButton I want to link to panel
cl.next(this); // searchView. Is that correct?
}
}
With this code, I get an IllegalArgumentException
Someone please point out my error! Also I have provided some comments for questions in the code.
As Always, Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将面板添加到卡片布局时,您没有使用任何约束来识别每张卡片。然后就可以直接跳转到具体的卡了。
有关工作示例,请参阅如何使用卡片布局。
You didn't use any constraints to identify each card when you added the panels to your card layout. Then you can jump directly to the specific card.
See How to Use Card Layout for a working example.
为了更清楚地说明,一些片段是从 @camickr
创建面板
时直接复制的,其中
BUTTONPANEL
和TEXTPANEL
是字符串。不同面板之间的切换是通过调用evt.getItem()
来完成的,其中evt.getItem()
将等于BUTTONPANEL
或TEXTPANEL
To make it even more clear, some snippets literally copied from the link provided by @camickr
Creation of the panel
where
BUTTONPANEL
andTEXTPANEL
are Strings. Switching between the different panels is done by callingwhere the
evt.getItem()
will be equal toBUTTONPANEL
orTEXTPANEL