编写一个 Java 类来为我的程序创建 CardLayouts

发布于 2024-12-23 12:22:00 字数 782 浏览 1 评论 0原文

我的程序中有一个选项卡式视图,在每个选项卡下有几个面板,我可以通过按钮在这些面板之间旋转。我决定为每个选项卡实现一个 CardLayout,考虑到我有大约 7 个选项卡,我决定编写一个类以使事情变得更整洁。该类称为PanelSystem,它接受已创建的JPanel 并将它们添加到CardLayout。我还将实现一个 switchPanel 方法来在面板之间移动。到目前为止,我已经:

public class PanelSystem {

JPanel cards;
CardLayout cl;

public PanelSystem(JPanel...panels) {
    // Create Panel with card layout
    cards = new JPanel(new CardLayout());

    // Add all the panels to the card system
    for (JPanel p : panels) cards.add(p);

    // Gains access to the card layout?
    cl = (CardLayout)(cards.getLayout());

    // Show starting card
    cl.show(cards, *UNIQUE IDENTIFIER*);
}
}

由于每个选项卡都有不同数量的 JPanel,我必须实现 JPanels...panels 行。我不确定这是否正常工作,但问题出现在构造函数的末尾,我试图显示第一张卡,因为由于我添加它们的方式,它没有唯一的标识符。关于如何解决这个问题有什么想法吗?预先感谢各位!

I have a tabbed view in my program and under each tab I have several panels which I rotate between with buttons. I've decided to implement a CardLayout for each one of these tabs, and given that I have about 7 tabs I decided to write a class to make things a bit neater. The class is called PanelSystem and it takes in JPanels which have already been created and adds them to a CardLayout. I will also implement a switchPanel method to move between panels. So far I have:

public class PanelSystem {

JPanel cards;
CardLayout cl;

public PanelSystem(JPanel...panels) {
    // Create Panel with card layout
    cards = new JPanel(new CardLayout());

    // Add all the panels to the card system
    for (JPanel p : panels) cards.add(p);

    // Gains access to the card layout?
    cl = (CardLayout)(cards.getLayout());

    // Show starting card
    cl.show(cards, *UNIQUE IDENTIFIER*);
}
}

Since there are different numbers of JPanels for each tab I had to implement the JPanels...panels line. I'm not sure if this works correctly yet, but the problem comes at the end of the constructor where I'm trying to show the first card since it doesn't have a unique identifier because of the way I added them. Any thoughts on how I could fix this?? Thanks in advance guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

ぺ禁宫浮华殁 2024-12-30 12:22:00

您可以像访问数组一样访问可变参数(无论如何,它确实是)。如果你用经典的 for 循环来循环它,

for (int i = 0; i < panels.length; i++) {
    cards.add(panels[i], Integer.toString(i));
}

然后在最后

cl.show(cards, "0");

编辑:我对 Swing 布局很生疏,无法准确记住你是否需要你的标识符是字符串还是任何对象,但你应该能够从这里弄清楚。

You can access the varargs as if it were an array (which it is, anyway). How about if you loop through it with a classical for loop

for (int i = 0; i < panels.length; i++) {
    cards.add(panels[i], Integer.toString(i));
}

then at the end

cl.show(cards, "0");

Edit: I'm rusty on Swing layouts, can't remember exactly whether you need your identifier to be a String or just any Object, but you should be able to figure it out from here.

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