编写一个 Java 类来为我的程序创建 CardLayouts
我的程序中有一个选项卡式视图,在每个选项卡下有几个面板,我可以通过按钮在这些面板之间旋转。我决定为每个选项卡实现一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以像访问数组一样访问可变参数(无论如何,它确实是)。如果你用经典的 for 循环来循环它,
然后在最后
编辑:我对 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
then at the end
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.