简体   繁体   English

如何退出Java Swing应用程序,避免从setInputVerifier生成的消息

[英]How to exit from Java Swing application, avoiding messages generated from setInputVerifier

I have a text field where I have imposed some constraint in input data, by the help of InputVerifier. 我有一个文本字段,我在InputVerifier的帮助下对输入数据施加了一些约束。 The constraint is if the text field is left empty, then an error message will pop up. 约束是如果文本字段留空,则会弹出错误消息。 When the application starts, the cursor remains on the text field (text_field.requestFocusInWindow()). 应用程序启动时,光标将保留在文本字段(text_field.requestFocusInWindow())上。

Now, If the exit button is clicked, at anytime, The application should terminate immediately. 现在,如果单击退出按钮,则应用程序应立即终止。 But if the text field remains empty, it displays the error message and doesn't exit. 但是,如果文本字段保持为空,则显示错误消息但不退出。 How to solve this problem? 如何解决这个问题呢?

Exit Code: 退出代码:

dispose();
System.exit(0);

Ok, let's say your textfield is in a JFrame. 好吧,假设您的文本字段位于JFrame中。 Then register a WindowEventListener on the frame. 然后在框架上注册一个WindowEventListener。 Catch the event that closes the window and remove the InputVerifiers from all textfields. 捕获关闭窗口的事件并从所有文本字段中删除InputVerifiers。 Then call System.exit(). 然后调用System.exit()。

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

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