如何在JOptionPane的ok按钮上添加监听器? [英] How can I add a listener on the ok button of JOptionPane?
问题描述
如何在点击 JOptionPane.INFORMATION_MESSAGE
的确定按钮时添加一个监听器。
How can I add a listener on the click of "OK" button of JOptionPane.INFORMATION_MESSAGE
.
我的JOptionPane是:
My JOptionPane is:
JOptionPane.showMessageDialog(null, "Your password is: " + password, "Your Password", JOptionPane.INFORMATION_MESSAGE);
推荐答案
showMessageDialog
方法在用户关闭或单击确定时返回void。但您可以使用方法 JOptionPane.showOptionDialog
,单击 DEFAULT_OPTION
表示确定按钮。如果单击确定,则 showOptionDialog
将返回0;如果用户关闭对话框,则返回-1。
The showMessageDialog
method returns void when the user closes or clicks ok. But you can use the method JOptionPane.showOptionDialog
with a single DEFAULT_OPTION
for the OK button. The showOptionDialog
will return 0 if OK was clicked and -1 if the user closed the dialog.
int res = JOptionPane.showOptionDialog(null, "Hello", "Test", JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null, null);
System.out.println(res);
你不需要监听器,因为 javadoc 说:
You don't need a listener because the javadoc says:
每个showXxxDialog方法都会阻止调用者,直到用户的交互完成。
Each showXxxDialog method blocks the caller until the user's interaction is complete.
这篇关于如何在JOptionPane的ok按钮上添加监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!