简体   繁体   English

我如何使用等待通知来停止在另一个类中等待按下按钮时暂停方法

[英]how do I use wait notify to stop pause a method while waiting on a button to be pressed in another class

So using a GUI in java, I am trying to allow the user to press a transfer button which will initiate a method. 因此,在Java中使用GUI,我试图允许用户按下转移按钮,这将启动方法。 This method will open up a separate GUI which will ask the user to enter information and then save it when the user has pressed a save button. 此方法将打开一个单独的GUI,该GUI将要求用户输入信息,然后在用户按下保存按钮时将其保存。 What I want to do is pause the method after it has opened the separate GUI and continue once the user has hit the save button in that second GUI. 我要做的是在打开单独的GUI后暂停该方法,并在用户单击第二个GUI中的“保存”按钮后继续。 Unless there is a better way, I believe that implementing a wait notify method is the best way to go about this but I have been unsuccessful thus far. 除非有更好的方法,否则我认为实现等待通知方法是解决此问题的最佳方法,但到目前为止,我一直没有成功。

Firstly, DON'T (use wait / notify ) for any reason within the Event Dispatching Thread. 首先,出于事件调度线程中的任何原因,请勿(使用wait / notify )。 This will cause the UI to stop responding to input events and repaint request, effectively "hanging" your program. 这将导致UI停止响应输入事件并重新绘制请求,从而有效地“挂起”程序。

In your case, it would be a simple case of using a modal dialog. 就您而言,这是使用模式对话框的简单情况。

Check out How to make dialogs and Concurrency in Swing 查看如何 在Swing中 进行对话框并发

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

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