如何在JOptionPane的ok按钮上添加监听器? [英] How can I add a listener on the ok button of JOptionPane?

查看:224
本文介绍了如何在JOptionPane的ok按钮上添加监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在点击 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆