使用 CardLayout 将自定义面板添加到 Applet

发布于 2024-12-19 15:27:21 字数 1102 浏览 2 评论 0原文

我无法弄清楚 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别念他 2024-12-26 15:27:21

将面板添加到卡片布局时,您没有使用任何约束来识别每张卡片。然后就可以直接跳转到具体的卡了。

有关工作示例,请参阅如何使用卡片布局

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.

ぇ气 2024-12-26 15:27:21

为了更清楚地说明,一些片段是从 @camickr

创建面板

//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

时直接复制的,其中 BUTTONPANELTEXTPANEL 是字符串。不同面板之间的切换是通过调用

CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());

evt.getItem() 来完成的,其中 evt.getItem() 将等于 BUTTONPANELTEXTPANEL

To make it even more clear, some snippets literally copied from the link provided by @camickr

Creation of the panel

//Create the panel that contains the "cards".
cards = new JPanel(new CardLayout());
cards.add(card1, BUTTONPANEL);
cards.add(card2, TEXTPANEL);

where BUTTONPANEL and TEXTPANEL are Strings. Switching between the different panels is done by calling

CardLayout cl = (CardLayout)(cards.getLayout());
cl.show(cards, (String)evt.getItem());

where the evt.getItem() will be equal to BUTTONPANEL or TEXTPANEL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文