简体   繁体   中英

How do I make it so JOptionPane.showMessageDialog can sense multiple strings?

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    String grade = JOptionPane.showInputDialog(null, "Please Specify Your Grade");
    String First_name = JOptionPane.showInputDialog(null, "What is your First Name?");
    String Last_name = JOptionPane.showInputDialog(null, "What is your Last Name?");
    ]JOptionPane.showMessageDialog (null,"You are a " + grade, "Your Name is " + First_name, "Your Last Name is " + Last_name);
}

How do I get the part where it says "Your Last Name is " + Last_Name to print the correct string? in my code it says that the string cannot be converted to an integer but the rest of that line works fine (using Netbeans IDE)

You are trying to pass multiple messages as separate parameters to the method JOptionPane.showMessageDialog , however the method only accepts a single message parameter. However, this parameter is not limited to just String s; you can actually pass any Object as the message. See the JOptionPane javadocs for details on how JOptionPane handles various types of message parameters.

I think there are a couple approaches that could be used. One approach is to create a String that concatenates all of the results together. My suggestion would be to use a newline character ( \\n ) to concatenate the results, so they appear one per line. Here is an example:

String message = "You are a " + grade + "\n"
    + "Your Name is " + First_name + "\n"
    + "Your Last Name is " + Last_name;
JOptionPane.showMessageDialog (null, message);

Another approach is to create an array out of the results, and pass the array as the message parameter:

String[] message = {
    "You are a " + grade,
    "Your Name is " + First_name,
    "Your Last Name is " + Last_name
};
JOptionPane.showMessageDialog (null, message);

works also in the constructor :

JOptionPane.showMessageDialog(null, "You are a " + grade+ "\nYour Name is " + First_name+
                "\nYour Last Name is " + Last_name);

and probably delete ] in your last line of code

JOptionPane has 3 showMessageDialog() methods, each with different arguments, let's look at each of them, but first we're going to use the following String :

String message = "You are a " + grade + " Your Name is " + First_name " Your Last Name is " + Last_name;

For more information you can check How to use Dialogs

Now, if you want to improve the format you can either use html tags as:

String message = "<html>" + name + "<br/>" + lastname + "<br/>" + grade + "</html>";

在此处输入图片说明

Or create your own custom JPanel where you add your components to it and then add that JPanel to the showMessageDialog on the Object message argument, but I'm leaving that part to you

This code will create the above output images, however you need to change the image path to your own path, the custom icon has been taken from here

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class DialogExamples {

    private JFrame frame;
    private ImageIcon icon = new ImageIcon("/home/jesus/Pictures/L5DGx.png");

    public static void main (String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DialogExamples().createAndShowGui();
            }
        });
    }
    public DialogExamples() {
        System.out.println(icon);
    }

    public void createAndShowGui() {

        frame = new JFrame("Example");

        String name = "Your name is Frakcool";
        String grade = "Your grade is 5";
        String lastname = "Your lastname is YajiSuzu";

        String message = "<html>" + name + "<br/>" + lastname + "<br/>" + grade + "</html>";
//      String message = name + " " + lastname + " " + grade;
        JOptionPane.showMessageDialog(frame, message);
        JOptionPane.showMessageDialog(frame, message, "My title", JOptionPane.QUESTION_MESSAGE);
        JOptionPane.showMessageDialog(frame, message, "My title2", JOptionPane.ERROR_MESSAGE, icon);

        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
}

In your case you're getting the exception:

String cannot be converted to an int

because you're sending 4 parameters here:

JOptionPane.showMessageDialog (null,"You are a " + grade, "Your Name is " + First_name, "Your Last Name is " + Last_name);

In this case, you're using the 2nd method I showed you above, so it's expecting to receive an int messageType on the 4th parameter, not a String.

This isn't something as a console.log() in JS, here you concatenate Strings with + operator, not with a , (comma).


NOTE:

As an aside note, your variable and method names should start with lowerCamelCase while your classes should start with UpperDromedaryCase as stated on the Java naming conventions


NOTE2:

You're not placing your program on the EDT which could cause you problems in the future, so be careful, my above code already solved that problem

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