message dialog box in java code example

Example 1: dialog box in java swing

import javax.swing.JOptionPane;

public class Main {
  public static void main(String[] argv) throws Exception {

    JOptionPane.showMessageDialog(null, "I am happy.");

  }
}

Example 2: java joptionpane

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

public class ShowMessageDialogExample1
{
  public static void main(String[] args)
  {
    String backupDir = "/Users/al/backups";
    
    // create a jframe
    JFrame frame = new JFrame("JOptionPane showMessageDialog example");
    
    // show a joptionpane dialog using showMessageDialog
    JOptionPane.showMessageDialog(frame,
        "Problem writing to backup directory: '" + backupDir + "'.");
    System.exit(0);
  }
}

Tags:

Java Example