Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how can i create a answer dialogbox simply? using java programing language

+5 votes
asked Nov 1, 2019 by Bhaskor Adhikari (230 points)
i need this code to make a answer dialog box in java programming language

1 Answer

0 votes
answered Aug 6, 2025 by Jerry Jeremiah (2,040 points)
You can use something like this:

import javax.swing.JOptionPane;

public class Main {
    public static void main(String[] args) {
        String message = "Hello World!";
        JOptionPane.showMessageDialog(null, message, "Message",
            JOptionPane.INFORMATION_MESSAGE);
    }
}

To show a dialog box in OnlineGDB, instead of clicking "Run" you need to choose the drop down beside it and choose "Run with display screen (beta)"
commented Aug 8, 2025 by lipikahuria (100 points)
import javax.swing.JOptionPane;

/**
 * A simple Java program that simulates a human-like conversation
 * using dialog boxes. It asks for the user's name, greets them,
 * asks how they are doing, and responds accordingly.
 */
public class HumanDialogExample {
    public static void main(String[] args) {
        
        // Greet the user with a friendly welcome message
        JOptionPane.showMessageDialog(
            null,
            " Hello there! I'm your friendly Java assistant.",
            "Welcome!",
            JOptionPane.INFORMATION_MESSAGE
        );

        // Ask for the user's name
        String name = JOptionPane.showInputDialog(
            null,
            "What's your name?",
            "Getting to Know You",
            JOptionPane.QUESTION_MESSAGE
        );

        // If the user provided a name
        if (name != null && !name.trim().isEmpty()) {
            // Trim the input to remove leading/trailing spaces
            name = name.trim();

            // Respond with a warm greeting
            JOptionPane.showMessageDialog(
                null,
                "Nice to meet you, " + name + "! ",
                "Greeting",
                JOptionPane.INFORMATION_MESSAGE
            );

            // Ask how the user is feeling
            String mood = JOptionPane.showInputDialog(
                null,
                "How are you feeling today, " + name + "?",
                "Your Mood",
                JOptionPane.QUESTION_MESSAGE
            );

            // Respond based on the user's mood input
            if (mood != null && !mood.trim().isEmpty()) {
                mood = mood.trim().toLowerCase();

                // Provide a simple human-like response based on mood
                if (mood.contains("good") || mood.contains("great") || mood.contains("fine") || mood.contains("happy")) {
                    JOptionPane.showMessageDialog(
                        null,
                        "That's awesome to hear! Keep smiling, " + name + " ",
                        "Glad to Hear!",
                        JOptionPane.INFORMATION_MESSAGE
                    );
                } else if (mood.contains("sad") || mood.contains("tired") || mood.contains("bad") || mood.contains("sick")) {
                    JOptionPane.showMessageDialog(
                        null,
                        "I'm sorry to hear that, " + name + ". I hope things get better soon. ",
                        "Cheer Up!",
                        JOptionPane.INFORMATION_MESSAGE
                    );
                } else {
                    JOptionPane.showMessageDialog(
                        null,
                        "Thanks for sharing, " + name + ". I'm always here to chat! ",
                        "Thanks",
                        JOptionPane.INFORMATION_MESSAGE
                    );
                }
            } else {
                // User didn’t enter a mood
                JOptionPane.showMessageDialog(
                    null,
                    "That's okay, " + name + ". Maybe next time you'll tell me how you're feeling. ",
                    "No Worries",
                    JOptionPane.INFORMATION_MESSAGE
                );
            }
        } else {
            // User didn't provide a name
            JOptionPane.showMessageDialog(
                null,
                "You didn't tell me your name... but that's okay! ",
                "No Name?",
                JOptionPane.INFORMATION_MESSAGE
            );
        }

        // End of conversation
        JOptionPane.showMessageDialog(
            null,
            "It was nice talking to you. Have a wonderful day! ",
            "Goodbye!",
            JOptionPane.PLAIN_MESSAGE
        );

        // End the program
        System.exit(0);
    }
}
}
     if this code really help please let me kown or any help need just me mail me [email protected]
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...