简体   繁体   English

在Java Swing中,当工作流不是线性的时,如何在JPanel之间切换?

[英]In Java Swing, how to switch between JPanels when the workflow isn't linear?

I want to create a JFrame that switches between multiple JPanels , but has a more complex structure than the linked list that CardLayout seems to offer. 我想创建一个在多个JPanels之间切换的JFrame ,但其结构比CardLayout似乎提供的链表更复杂。 Is there something that offers more of a directed graph based layout? 有什么东西可以提供更多基于有向图的布局吗?

Here's the scenario I'm thinking of: 这是我正在考虑的方案:

 ----------------------------  ->  ---------------
| Select User / New User Btn |    | New User Form |
 ----------------------------  <-  ---------------
     |         
     V
 --------
| Form 1 |
 --------
     |
     V
 --------       --------------
| Form 2 |  -> | Results Page |
 --------       --------------

As you can see this isn't a linked list, but a directed graph. 如您所见,这不是链接列表,而是有向图。 Is there a standard way to deal with this scenario? 有没有解决这种情况的标准方法?

I want to create a JFrame that switches between multiple JPanels, but has a more complex structure than the linked list that CardLayout seems to offer 我想创建一个在多个JPanel之间切换的JFrame,但其结构比CardLayout似乎提供的链表更复杂

You are not forced to view the panels in a CardLayout in linear order. 您不必强制以CardLayout线性顺序查看面板。

You can display any panel you wish by specifying the panel name you wish to display by using the show(...) method. 您可以通过使用show(...)方法指定要显示的面板名称来显示任何想要的面板。 Look at the example from the Swing tutorial on How to Use Card Layout for a working example that uses this approach. 请查看Swing教程中有关如何使用卡布局的示例,以了解使用此方法的有效示例。

The CardLayout simply provides cards, it is up to you to determine which card should be visible. CardLayout仅提供卡片,由您决定哪个卡片应可见。 It provides some ordered-list style API methods for convenience (presumably because the designers decided that that was one of the more common use cases) but you don't have to use them. 为了方便起见,它提供了一些有序列表样式的API方法(大概是因为设计者认为这是最常见的用例之一),但是您不必使用它们。

Implement flow control in your buttons (or in whatever code you're using to determine the behavior here) and just use CardLayout.show() to show the appropriate card. 在您的按钮(或用于确定行为的代码)中实现流控制,只需使用CardLayout.show()即可显示相应的卡。 You don't have to use first() , last() , previous() , and next() if you don't want to. 不必使用first() last() previous()next() ,如果你不想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM