Displaying a simple message box or an alert dialog with an Ok button can be done with just one line of code as show below:


A) Without a JFrame

JOptionPane.showMessageDialog(null, “Message goes here”);

This will display the dialog box in the center of your screen.


B) If you have a JFrame

JOptionPane.showMessageDialog(frame, “Message goes here”);

This will display the dialog box in the center of your JFrame