Java Swing卡片布局-显示功能
我正在使用卡片布局来创建我的应用程序, 我的两张卡片正在剪切相同的信息(在一张卡片中,您将数据输入到文本区域,而在另一张卡片中,数据被打印)。 数据保存在框架中。 我在第二张卡上添加了刷新功能来获取值并打印它,但我不知道如何触发它。
有没有可以绑定到这个函数的事件? 或者有任何其他方式从框架访问卡片功能(这样我每次展示卡片时都可以触发它)?
谢谢'
I'm using the card layout to create my app,
2 of my cards are shearing the same information (in one you enter data to textarea and in the other the data is printed).
The data is been saved in the Frame.
I have added a refresh function to the second card that get the value and print it, But I don't know how to trigger it.
Is there any event that can be bind to this function?
Or any other way to get to the card functions from the frame (so I can trigger it every time I show the card)?
Thanks'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常有两种方法可以实现您所追求的目标。要么按照您的建议,在显示时更新/刷新输出卡。这可以通过添加 ComponentListener 来完成 到用作输出卡的组件。这样您就可以覆盖 componentShown 方法,以便它调用您的自定义刷新方法。请参阅此问题的已接受答案。
第二种选择是每当任何数据发生更改时调用刷新 - 始终保持最新状态,即使它实际上不可见。根据您的应用程序的外观,这可能会以不同的方式完成。如果您有专用的数据模型,那么您可以使用 观察者 / Observable 模式用于通知内部模型的更改,或者您可以对模型进行硬编码以在更改时调用刷新。如果您只想直接从第一张卡上的输入字段获取数据,那么您可以将该代码添加到各自的更改侦听器等中。
There are generally two ways of achieving what you are after. Either you update/refresh the output card whenever it is displayed, like you suggested. This can be done by adding a ComponentListener to the Component that you use as your output card. This way you can override the componentShown method so that it calls your custom refresh method. See the accepted answer for this question.
The second alternative is to call refresh whenever any of the data is changed - always keeping it up to date, even when it is not actually visible. Depending on how your application looks this might be done in different ways. If you have a dedicated data model then you could use the Observer / Observable pattern to notify changed from an internal model, or you could hard-code the model to call refresh whenever it is changed. If you just want to get the data directly from the input fields on the first card, then you could just add that code into their respective change listeners etc.