如何使Swing / JFace / SWT GUI可寻址?

时间:2022-11-11 17:26:24

I have a "fat" GUI that it getting fairly complex, and I would like to add links from a place to an other, and add back/forward buttons to ease navigation. It seems to me that this would be easier if my application was addressable: each composite could have its URI, and links would use that URI.

我有一个“胖”的GUI,它变得相当复杂,我想添加从一个地方到另一个地方的链接,并添加后退/前进按钮以方便导航。在我看来,如果我的应用程序是可寻址的,这将更容易:每个复合可以有它的URI,链接将使用该URI。

Are there design patterns applicable to this problem?

是否有适用于此问题的设计模式?

I could just look at the source code for Firefox or Eclipse, but these are huge projects and it would take a good amount of time making sense of it, so I'm asking here. Is there a simpler example somewhere?

我可以看看Firefox或Eclipse的源代码,但这些都是巨大的项目,需要花费大量时间来理解它,所以我在这里问。在某处有一个更简单的例子吗?

Of course it would be simpler if I had build a web app in the first place, but I'm not going to rewrite this huge app from scratch anytime soon.

当然,如果我首先构建一个Web应用程序会更简单,但我不会很快从头开始重写这个巨大的应用程序。

4 个解决方案

#1


0  

In Swing, you might use a CardLayout. You can have each "page" be a card, and the name of the card (chosen when adding cards to the layout) would be equivalent to the URI you want.

在Swing中,您可以使用CardLayout。您可以将每个“页面”作为卡片,并且卡片的名称(在向布局添加卡片时选择)将等同于您想要的URI。

Example:

String PAGE_1_KEY = "page 1";
String PAGE_2_KEY = "page 2";
// as many keys as you need

JFrame frame = ...;
frame.setLayout(new CardLayout());
frame.add(createPage1(), PAGE_1_KEY);
frame.add(createPage2(), PAGE_2_KEY);
// etc.

Then in your buttons' action listeners, you would call

然后在你的按钮的动作听众中,你会打电话

((CardLayout)frame.getLayout()).show(frame, PAGE_1_KEY); // or whichever

#2


1  

My solution for doing things like this usually involves the listener pattern. In a nutshell, you reduce coupling by providing a way to send and receive events to and from interested parties (composites in this case). This is fairly easy to implement, even when retrofitting. This way, your events and parties can change without changing the dependent code.

我做这样的事情的解决方案通常涉及监听器模式。简而言之,您通过提供一种向感兴趣的各方发送和接收事件的方式来减少耦合(在这种情况下是复合材料)。即使在改装时,这也很容易实现。这样,您的事件和参与方可以在不更改相关代码的情况下进行更改。

#3


1  

You could build a "global" registry that maps unique IDs to objects (or maybe class names). These objects could be JPanels, for instance. When the user clicks on a link or button, some Controller is notified with the key of the new page to be displayed. This Controller could create that JPanel and place it in the application's frame.

您可以构建一个“全局”注册表,将唯一ID映射到对象(或类名)。例如,这些对象可以是JPanel。当用户单击链接或按钮时,将向某些Controller通知要显示的新页面的键。此Controller可以创建该JPanel并将其放在应用程序的框架中。

#4


1  

My last approach included global manager and registration of links. Each part of UI was able to name yourself uniquely and register. The global manager knows about each one and then use some dirty work to bring this part visible. The back/forward navigation was made by special undo/redo manager. Each "display" was able to obtain navigation manager and register its "undo" event. It was hard work to make it work, but the resulting feature was quite useful. We discussed about using some simple JNDI service to locate and name UI parts. It may be useful in linking too.

我的最后一种方法包括全球经理和链接注册。 UI的每个部分都能够为自己命名并注册。全局经理知道每一个,然后使用一些肮脏的工作来使这部分可见。后退/前进导航由特殊的撤消/重做管理器完成。每个“显示器”都能够获得导航管理器并注册其“撤消”事件。让它工作很辛苦,但最终的功能非常有用。我们讨论了使用一些简单的JNDI服务来定位和命名UI部分。它也可能在链接中很有用。

#1


0  

In Swing, you might use a CardLayout. You can have each "page" be a card, and the name of the card (chosen when adding cards to the layout) would be equivalent to the URI you want.

在Swing中,您可以使用CardLayout。您可以将每个“页面”作为卡片,并且卡片的名称(在向布局添加卡片时选择)将等同于您想要的URI。

Example:

String PAGE_1_KEY = "page 1";
String PAGE_2_KEY = "page 2";
// as many keys as you need

JFrame frame = ...;
frame.setLayout(new CardLayout());
frame.add(createPage1(), PAGE_1_KEY);
frame.add(createPage2(), PAGE_2_KEY);
// etc.

Then in your buttons' action listeners, you would call

然后在你的按钮的动作听众中,你会打电话

((CardLayout)frame.getLayout()).show(frame, PAGE_1_KEY); // or whichever

#2


1  

My solution for doing things like this usually involves the listener pattern. In a nutshell, you reduce coupling by providing a way to send and receive events to and from interested parties (composites in this case). This is fairly easy to implement, even when retrofitting. This way, your events and parties can change without changing the dependent code.

我做这样的事情的解决方案通常涉及监听器模式。简而言之,您通过提供一种向感兴趣的各方发送和接收事件的方式来减少耦合(在这种情况下是复合材料)。即使在改装时,这也很容易实现。这样,您的事件和参与方可以在不更改相关代码的情况下进行更改。

#3


1  

You could build a "global" registry that maps unique IDs to objects (or maybe class names). These objects could be JPanels, for instance. When the user clicks on a link or button, some Controller is notified with the key of the new page to be displayed. This Controller could create that JPanel and place it in the application's frame.

您可以构建一个“全局”注册表,将唯一ID映射到对象(或类名)。例如,这些对象可以是JPanel。当用户单击链接或按钮时,将向某些Controller通知要显示的新页面的键。此Controller可以创建该JPanel并将其放在应用程序的框架中。

#4


1  

My last approach included global manager and registration of links. Each part of UI was able to name yourself uniquely and register. The global manager knows about each one and then use some dirty work to bring this part visible. The back/forward navigation was made by special undo/redo manager. Each "display" was able to obtain navigation manager and register its "undo" event. It was hard work to make it work, but the resulting feature was quite useful. We discussed about using some simple JNDI service to locate and name UI parts. It may be useful in linking too.

我的最后一种方法包括全球经理和链接注册。 UI的每个部分都能够为自己命名并注册。全局经理知道每一个,然后使用一些肮脏的工作来使这部分可见。后退/前进导航由特殊的撤消/重做管理器完成。每个“显示器”都能够获得导航管理器并注册其“撤消”事件。让它工作很辛苦,但最终的功能非常有用。我们讨论了使用一些简单的JNDI服务来定位和命名UI部分。它也可能在链接中很有用。