我无法找出 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
请大家指出我的错误!我还针对代码中的问题提供了一些评论。 一如既往,谢谢!
最佳答案
将面板添加到卡片布局时,您没有使用任何约束来识别每张卡片。然后就可以直接跳转到具体的卡片了。
参见How to Use Card Layout一个工作示例。
关于java - 使用 CardLayout 将自定义面板添加到 Applet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8380365/