显示 CardLayout 的上一个选项卡

发布于 2024-10-02 05:13:14 字数 555 浏览 7 评论 0原文

我很难弄清楚这一点。

这种情况并不难想象,所以我希望我可以在没有示例代码的情况下发布此内容。 我有 JPanel 和 CardLayout。该面板包含几个其他面板作为单独的选项卡。我想要实现的是创建一个 JButton,它将使主面板显示最后打开的选项卡(显示当前选项卡之前的选项卡)。一开始我以为没什么大不了的。

所以我开始思考如何获取当前可见的选项卡(面板),并发现在Panel或CardLayout中没有本地方法,只包含方法first(),last(),next()和previous()这对我来说没有用。

然后我想也许为了实现我的目标,我可能会检查主面板的所有组件并测试哪一个是可见的,哪一个是当前的。我发现它有点复杂,很好,但是如果我没有它的卡片名称来调用 CardLayout 方法 show(parent, name),如何让这个面板通过 CardLayout 显示呢?

在这一点上,我不知道。我一直在互联网上搜索,但没有找到解决方案,只是很少有人试图找出类似的解决方案。 所以我希望已经处理过此类问题的人能够遇到这个主题:-)

感谢您的任何建议。

最好的问候

马丁·S.

I'm having trouble figuring this one out.

The situation is no difficult to imagine so I hope I could post this without a sample code.
I have JPanel with CardLayout. The panel contains several other panels as individual tabs. What I'm trying to achive is to create a JButton that will make the main panel show last opened tab (the one before the current was shown). No big deal I thought at first.

So I started by thinking how to get the current visible tab (panel) and found out that there is no native method for that in Panel or rather CardLayout that only contains methods first(), last(), next() and previous() which are no use to me.

Then I thought that maybe to achive my goal I might go through all components of the main panel and test which one is visible which would be the current one. I find it a little complicated, fine, but how do I make this panel show by CardLayout if I don't have its card name to call CardLayout method show(parent, name)?

At this point, I have no idea. I've been searching over the Internet but haven't found a solution, only that few people have been trying to figure something similar out.
So I hope someone who already delt with this kind of issue comes across this topic :-)

Thanks for any suggestions.

Best regards

Martin S.

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

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

发布评论

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

评论(2

南七夏 2024-10-09 05:13:14

这就提出了一个问题:您最初是如何获得当前卡片的。如果您使用 next() 获取当前卡,则 previous() 将获取上一张卡,反之亦然。如果您使用 first()last(),那么您就不走运了。

当然,您始终可以复制 CardLayout 存储的一些信息来实现您的目标:如果您维护一个组件名称数组(并在调用 addLayoutComponent 时更新它) )以及先前的索引(并使用 nextprevious 和其他调用更新此索引),那么您只需查找前一个组件的名称并使用 show 显示上一张卡片。


更新:由于您知道选项卡名称,因此可以通过以下方式维护以前的选项卡名称:

String prevTab, curTab;

public void showTab(String newTab) {
  prevTab = curTab;
  curTab = newTab;
  layout.show(parent, curTab);
}

public void goBack() {
  layout.show(parent, prevTab);
}

This brings up the question of how you arrived at the current card in the first place. If you used next() to get to the current card, then previous() will get you to the previous card and vice versa. If you use first() or last(), then you're out of luck.

Of course, you could always duplicate some of the information stored by the CardLayout to achieve your goal: if you maintain an array of component names (and update it whenever you call addLayoutComponent) as well as a previous index (and update this with next, previous and other calls), then you can just lookup the name of the previous component and use show to show the previous card.


Update: Since you know the tab names, here's how you can maintain the previous tab name:

String prevTab, curTab;

public void showTab(String newTab) {
  prevTab = curTab;
  curTab = newTab;
  layout.show(parent, curTab);
}

public void goBack() {
  layout.show(parent, prevTab);
}
行雁书 2024-10-09 05:13:14

您可以创建一个地图对象Map。并添加面板和名称到卡片布局和地图。之后,如果您有之前显示的 JPanel,则可以通过 map.get(JPanel p) 从地图中获取其名称。
要查找之前可见的 JPanel,您需要将 ComponentListener 添加到每个 JPanel。并使其更改 componentHidden 事件上的一些静态变量(例如静态 JPanel prev_shown)。

You can create a map object Map. And add panels & names to card layout and to the map. Afterwards, if you have the previously shown JPanel, you get its' name from map via map.get(JPanel p).
And to find what JPanel was visible before the moment, you'll need to add ComponentListener to each JPanel. And make it to change some static variable on componentHidden event (e.g. static JPanel prev_shown).

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