简体   繁体   中英

JOptionPane how to exit prompt

import javax.swing.*;

public class TestProject {
    public static void main(String[] args) {

        JOptionPane.showMessageDialog(null, "Just about to draw the best piece of artwork in my life, need input please.");

        String name= JOptionPane.showInputDialog("Imagine a car, what is it's name?");
        JOptionPane.showMessageDialog(null, "Ah okay, so it's name is " + name);


        String bodyColour = JOptionPane.showInputDialog("Cool. What colour was the body of the car? \n Blue, Red, Black, Green or Orange? (More to come in DLC's) ");
        JOptionPane.showMessageDialog(null, "Mhmm");

        String wheelColour= JOptionPane.showInputDialog("Colour of the wheels? ");
        JOptionPane.showMessageDialog(null, "Very ordinary...");

    String doorColour = JOptionPane.showInputDialog("Last... And probably least, colour of the door?");
        JOptionPane.showMessageDialog(null, "Finished the drawing!");

    }
}

That is my code above, and I was wondering how the user can press the cancel, (because normally it acts like the user pressed 'ok') or the x button in the pop-up prompt, I imagine it could use some type of listener or perhaps System.exit(0); but I am not sure how to implement it as I'm fairly new to Java

This may solve your problem

   if(name == null || (name != null && ("".equals(name))))   
   {
     System.exit(0);// or you can throw exception
    }

write this line of code just after your first JOptionPane.showInputDialog()

Your code (just added the above snippet)

import javax.swing.*;

public class TestProject {
public static void main(String[] args) {

    JOptionPane.showMessageDialog(null, "Just about to draw the best piece of artwork in my life, need input please.");

    String name= JOptionPane.showInputDialog("Imagine a car, what is it's name?");
    if(name == null || (name != null && ("".equals(name))))   
    {
      System.exit(0);// or you can throw exception
    }
    JOptionPane.showMessageDialog(null, "Ah okay, so it's name is " + name);


    String bodyColour = JOptionPane.showInputDialog("Cool. What colour was the body of the car? \n Blue, Red, Black, Green or Orange? (More to come in DLC's) ");
    JOptionPane.showMessageDialog(null, "Mhmm");

    String wheelColour= JOptionPane.showInputDialog("Colour of the wheels? ");
    JOptionPane.showMessageDialog(null, "Very ordinary...");

    String doorColour = JOptionPane.showInputDialog("Last... And probably least, colour of the door?");
    JOptionPane.showMessageDialog(null, "Finished the drawing!");

 }
}

如果用户在提示上按“取消”或x按钮,则showInputDialog将返回null。

Use confirmDialog like this:

int answer = JOptionPane.showConfirmDialog(null, "Your Message", "Your Title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);

if(answer == JOptionPane.CANCEL_OPTION){
    System.out.println("canceled");
} else {
    System.out.println("OK");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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