简体   繁体   English

在CardLayout等中使用ActionListener重用动态JList

[英]Reusing a Dynamic JList with actionlisteners in CardLayout etc

I am creating a JList dynamically and I want to use it on different JPanels where I can call a setQuery(query); 我正在动态创建一个JList,我想在不同的JPanels上使用它,我可以在其中调用setQuery(query); on it. 在上面。 There is a Search Button + field connected to it too. 也有一个搜索按钮+字段连接到它。 Is there a way to place the JList, search button and search field on like 10 different panels without having to duplicate the code everywhere? 有没有一种方法可以将JList,搜索按钮和搜索字段放置在10个不同的面板上,而不必到处重复代码? I can't place it on a seperate JPanel as it has other buttons / gui elements that need be around it depending on what JPanel is being displayed. 我不能将其放在单独的JPanel上,因为它周围还需要其他按钮/ gui元素,具体取决于所显示的JPanel。 I've looked everywhere but there is not much mention of reusing a dynamic JList. 我到处都看过,但没有太多提及重用动态JList。

You can place a component in one visualized container only. 您只能将组件放置在一个可视化容器中。 Consider creating a method that creates these components for you, that returns a JPanel that holds the components, and perhaps have all created JLists share the same ListModel, and all JButtons the same AbstractAction. 考虑创建一个为您创建这些组件的方法,该方法返回一个保存组件的JPanel,也许所有创建的JList共享相同的ListModel,所有JButton共享相同的AbstractAction。

Or another option, if you are using CardLayout to swap JPanels, and you want all of the panels to hold these components above, consider not doing this but moving the JList, JButton, and JTextField out of the cards and on to a less dynamic portion of your GUI. 另一个选择是,如果您使用CardLayout交换JPanels,并且希望所有面板都在上面保留这些组件,请考虑不执行此操作,而是将JList,JButton和JTextField从卡中移出,并移至动态性较低的部分您的GUI。

Also, this is not clear to me: 另外,这对我来说还不清楚:

I can't place it on a seperate JPanel as it has other buttons / gui elements that need be around it depending on what JPanel is being displayed. 我不能将其放在单独的JPanel上,因为它周围还需要其他按钮/ gui元素,具体取决于所显示的JPanel。

Please clarify and tell us more. 请澄清并告诉我们更多。 Images might help as well. 图像可能也有帮助。

还请考虑装饰器模式您的列表/搜索面板将是主要组件,而装饰原始器将导致变化。

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

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