使用 Java Swing 应用程序,如何将对话框面板中的值获取到主应用程序中?
在我的 Swing 应用程序中,用户可以单击按钮打开对话框面板并输入一些值,然后他们可以单击该面板上的“确定”将其关闭并返回主程序,但是我如何将他们输入的值传递给主程序而不先将它们保存到文件中?
In my Swing app, users can click a button to open a dialog panel and enter some values, then they can click "Ok" on that panel to close it and return to the main program, but how can I pass the values they enter to the main program without saving them to a file first ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以执行以下操作:
setVisible(false)
时,您可以查询对话框的数据。There are a couple things you could do:
setVisible(false)
on dialog, you can query the dialog for its data.UI 通常应该分层在底层应用程序之上。 (IMO,UI 本身应该分为几层,但这是另一个咆哮。)将应用程序的相关部分传递到 UI 的创建中。当您准备好将值从 UI 传输到应用程序(可以是多页对话框上的“确定”按钮,直至每次击键)时,请将数据推送到应用程序组件。
The UI should usually be layered upon the underlying application. (IMO, the UI should itself be split into layers, but that another rant.) Pass in a relevant part of your application to the creation of your UI. When you are ready for values to go from the UI to the application (which could be an OK button on a multi-page dialog, down to every keystroke), push the data to the application component.
我宁愿建议您看一下“事件总线”,而不是仅仅使用糟糕的 Java
Observable
/Observer
API,它应该特别适合您的情况。事件总线的两种实现:
可以在任何情况下使用
其中之一应该帮助您解决当前的问题。
Rather than using just the poor Java
Observable
/Observer
API, I'd rather advise you take a look at an "Event Bus", which should be particularly suited for your situation.Two implementations of Event Buses:
can be used in any situation
One of these should help you with your current problem.
只需在对话框类中定义一个具有单个方法的接口(例如:“returnToCaller(returntype rt)”)即可。
对话框的构造函数将此接口的实例作为输入,并使用它在退出/确定时返回结果。
主模块(窗口或其他)只是实例化对话框,从而匿名使用该接口,定义返回方法,有点像委托(c#)。
那么调用是:
Just define an interface with a single method (like: "returnToCaller(returntype rt)") in your dialog class.
The Constructor of the dialog takes an instance of this interface as input, and uses it to return results on exit/ok.
The mainmodule (window or whatever) simply instantiates the dialog and thus makes annonymous use of the interface, defininng the return method, sort of like a delegate (c#).
The call then being:
我建议采用MVC(模型-视图-控制器)设计。所以对话框将是你的视图,也可能是控制器。但必须有一个域类作为您的模型。例如,如果创建对话框是为了编辑个人数据,那么您可以使用名为 Person 的类来保存数据。对话框的设计方式应使您可以从中设置和获取人员。
I would suggest MVC(Model-view-controller) design. So dialog will be you view and possibly controller. But have to have a domain class which will be your model. For example, if the dialog is created to edit personal data, then you can have class called Person which will hold the data. The dialog should be designed in the way so you can set and get a Person from it.
实现对话框面板的类必须具有到主程序的链接,并且主程序必须提供一个带有参数的方法,这些参数将是要传输的值。
然后,您的对话框面板类侦听“确定”按钮,并在单击按钮时检索值并通过上述方法使用它们。
The class implementing your dialog panel must have a link to your main program, and your main program must provide a method with parameters that will be the values to transmit.
Then your dialog panel class listen to the Ok button, and on the button click, it retrieve the values and use them with the aforementionned method.
也许您想尝试这个解决方案:
class MyDialog {
}
May be you would like to try this solution:
class MyDialog {
}