简体   繁体   中英

show input dialog box after error message dialog box

I need to display an input dialog box and enter the player's name.

If the player clicked the ok button and the value of the input dialog is either a number or null an error should be displayed. If the player clicks the ok button of the error message dialog box, the input box will appear again.

I don't know if my code is wrong. I forgot how to code in java because of my web programming subject.

int[] player = new int[1];
for(int a =0; a<player.length; a++){
    String input = JOptionPane.showInputDialog("Enter your Name:",JOptionPane.OK_CANCEL_OPTION); 

    try {
        if(!input.matches("[a-zA-Z]+")){
            JOptionPane.showMessageDialog(null,"Use Letters only", "Warning", JOptionPane.OK_OPTION);    
        } else {
            input = String.valueOf(player[a]);
            category c = new category();
            this.dispose();
            c.show();
        }
    }
    catch(Exception e){   
};

If you do the loop right, you'll need only one dialog :

    String message = "Enter Your Name:";
    String playerName = null;
    do {
        playerName =
            JOptionPane.showInputDialog(message);
        message = "<html><b style='color:red'>Enter Your Name:</b><br>"
                + "Use letters only.";          
    } while(playerName != null && !playerName.matches("[a-zA-Z]+"));

    System.out.println("PlayerName: " + playerName);

This will include the error message into the next iteration of asking for a valid name. Makes it a bit nicer to work with as well.

Shows like this on first call:

第一次迭代

and like this in case of errors:

如果有错误

Note

You might want to change your regex to "[a-zA-Z]\\\\w+" if numbers later on in the name are okay (for instance "bunny99" )

Show an error dialog that displays the message, 'alert':

JOptionPane.showMessageDialog(null, "alert", "alert", JOptionPane.ERROR_MESSAGE);

Show an internal information dialog with the message, 'information':

JOptionPane.showInternalMessageDialog(frame, "information", "information", JOptionPane.INFORMATION_MESSAGE);

Show an information panel with the options yes/no and message 'choose one':

JOptionPane.showConfirmDialog(null,"choose one", "choose one", JOptionPane.YES_NO_OPTION);

Show an internal information dialog with the options yes/no/cancel and message 'please choose one' and title information:

JOptionPane.showInternalConfirmDialog(frame, "please choose one", "information",JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.INFORMATION_MESSAGE);

Show a warning dialog with the options OK, CANCEL, title 'Warning', and message 'Click OK to continue':

Object[] options = { "OK", "CANCEL" };
JOptionPane.showOptionDialog(null, "Click OK to continue", "Warning",JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,null, options, options[0]);

Show a dialog asking the user to type in a String:

String inputValue = JOptionPane.showInputDialog("Please input a value");

Show a dialog asking the user to select a String:

Object[] possibleValues = { "First", "Second", "Third" };
Object selectedValue = JOptionPane.showInputDialog(null,"Choose one", "Input",JOptionPane.INFORMATION_MESSAGE, null, possibleValues, possibleValues[0]);

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