简体   繁体   English

在JOptionPane.showMessageDialog上如何关闭?

[英]How to close when on JOptionPane.showMessageDialog?

I have a JOptionPane set when a certain action happens, and I want it to be showMessageDialog because that just displays a message. 当某个动作发生时,我设置了一个JOptionPane,我希望它是showMessageDialog,因为它只显示一条消息。 How do I make it so when you click OK or cancel, it closes the entire application? 在单击“确定”或“取消”时,如何关闭整个应用程序?

This is what I put 这就是我写的

JOptionPane.showMessageDialog(null, "you lose");
if (JOptionPane.OK_OPTION){
        System.exit(0);
}

but it doesnt work. 但它不起作用。 I based it off of this code I found online: 我将其基于在网上找到的以下代码:

int exit = JOptionPane.showConfirmDialog(mainFrame, "Are you sure?");

if (exit == JOptionPane.YES_OPTION)
{
    try
    {
        Runtime.getRuntime().exec("cmd.exe /c start www.yahoo.com");
    }
    catch (Exception exception)
    {
    }
    System.exit(0);
}
else {
}

But that is for a Confirm dialog and I just want it to work for Message dialog. 但这是用于“确认”对话框的,我只希望它可用于“消息”对话框。

I tried changing JOptionFrame.showConfirmDialog to JOptionFrame.showMessageDialog but it doesnt work, as int and other stuff had to be deleted. 我尝试将JOptionFrame.showConfirmDialog更改为JOptionFrame.showMessageDialog,但它不起作用,因为int和其他内容必须删除。

Thanks 谢谢

You can use ConfirmaDialog and use the INFORMATION_MESSAGE type. 您可以使用ConfirmaDialog并使用INFORMATION_MESSAGE类型。

int exit = JOptionPane.showConfirmDialog(null, "Are you sure?" , null, JOptionPane.OK_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);
if (exit == JOptionPane.YES_OPTION)
{
    //Do smt
}

If you will close the app any way, just do this: 如果您将以任何方式关闭应用程序,请执行以下操作:

JOptionPane.showMessageDialog(null, "you lose");
System.exit(0);

Why need to bother if in any cases, you will close the app? 在任何情况下,您都需要关闭应用程序,为什么还要打扰呢?

Try this: 尝试这个:

int choice = JOptionPane.showConfirmDialog(null, "you lose", "title", JOptionPane.OK_CANCEL_OPTION);
if (choice == JOptionPane.OK_OPTION || choice == JOptionPane.CANCEL_OPTION){
   System.exit(0);
}

Thanks. 谢谢。

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

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