如何捕获从 JOptionPane 按下的确定按钮
how to capture ok button pressed from JOptionPane
我想在 JOptionPane 上按下 "OK" 按钮时捕获确定按钮事件。然后我想显示一个 jframe。我找到了许多关于捕获除 JOptionPane 之外的各种事件的教程和视频。 Java 文档对新手帮助不大。希望有人能帮忙。我有以下内容。
JOptionPane.showMessageDialog(frame,
"Press OK to get a frame");
如何实现侦听器来捕获 OK 按下事件。
private class Listener implements ActionListener {
public void
actionPerformed(ActionEvent e) {
}
}
无需捕获它 -- 代码流将 return 立即 post JOptionPane
显示行。如果你想知道是确定还是取消还是删除 window 被按下,那么使用不同的 JOptionPane
——使用 JOptionPane.showConfirmDialog(...)
,并捕获结果 returned此方法调用。
String text = "Press OK to get a frame";
String title = "Show Frame";
int optionType = JOptionPane.OK_CANCEL_OPTION;
int result = JOptionPane.showConfirmDialog(null, text, title, optionType);
if (result == JOptionPane.OK_OPTION) {
//...
}
你不能用showMessageDialog
方法来做。您必须改用 showConfirmDialog
方法。这将为您 return 您提供一个值,您可以根据该值确定按下的按钮:
int result = JOptionPane.showConfirmDialog(frame, "Press OK to get a frame");
if (result == JOptionPane.YES_OPTION) {
// Yes button was pressed
} else if (result == JOptionPane.NO_OPTION) {
// No button was pressed
}
要获得确定按钮,您需要使用 OK_CANCEL_OPTION
:
int result = JOptionPane.showConfirmDialog(frame, "Press OK to get a frame",
"Title", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
// OK button was pressd
} else if (result == JOptionPane.CANCEL_OPTION) {
// Cancel button was pressed
}
我想在 JOptionPane 上按下 "OK" 按钮时捕获确定按钮事件。然后我想显示一个 jframe。我找到了许多关于捕获除 JOptionPane 之外的各种事件的教程和视频。 Java 文档对新手帮助不大。希望有人能帮忙。我有以下内容。
JOptionPane.showMessageDialog(frame,
"Press OK to get a frame");
如何实现侦听器来捕获 OK 按下事件。
private class Listener implements ActionListener {
public void
actionPerformed(ActionEvent e) {
}
}
无需捕获它 -- 代码流将 return 立即 post JOptionPane
显示行。如果你想知道是确定还是取消还是删除 window 被按下,那么使用不同的 JOptionPane
——使用 JOptionPane.showConfirmDialog(...)
,并捕获结果 returned此方法调用。
String text = "Press OK to get a frame";
String title = "Show Frame";
int optionType = JOptionPane.OK_CANCEL_OPTION;
int result = JOptionPane.showConfirmDialog(null, text, title, optionType);
if (result == JOptionPane.OK_OPTION) {
//...
}
你不能用showMessageDialog
方法来做。您必须改用 showConfirmDialog
方法。这将为您 return 您提供一个值,您可以根据该值确定按下的按钮:
int result = JOptionPane.showConfirmDialog(frame, "Press OK to get a frame");
if (result == JOptionPane.YES_OPTION) {
// Yes button was pressed
} else if (result == JOptionPane.NO_OPTION) {
// No button was pressed
}
要获得确定按钮,您需要使用 OK_CANCEL_OPTION
:
int result = JOptionPane.showConfirmDialog(frame, "Press OK to get a frame",
"Title", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
// OK button was pressd
} else if (result == JOptionPane.CANCEL_OPTION) {
// Cancel button was pressed
}