Search code examples
javawindowscygwin

Confirmation Box in Java | Compilation Error


I'm a new programmer and I'm having issues when I compile the following code:

import java.awt.*;
import java.awt.event.*;
import java.swing.*;

public class HelloWorld{
    public static void main(String[] args){

    int UserExit = JOptionPane.showConfirmDialog(null, "Do you really want to exit?", "Confirmation", JOptionPane.YES_NO_OPTION);
        if (UserExit == JOptionPane.YES_OPTION) {
          JOptionPane.showMessageDialog(null, "Goodbye!");
        }
        else {
           JOptionPane.showMessageDialog(null, "Too late my friend!");
           System.exit(0);
        }

        System.exit(0);
    }
}

I have checked it many times but cannot find the mistake. Cygwin doesn't show the mistake and blames something different every time. I use cygwin as the compilation tool and my OS is Windows 8.1 x64 Bit.

Any help would be very welcome.

EDIT I use javac to compile code in Java, I meant I use cygwin as it's platform. I apologize.

EDIT The compiler was continiously "blaming" the libraries, the mistake was a wrongly typed class. Thanks to @kagmole and @RealSkeptic for helping me out :)


Solution

  • The swing components are inside javax, not java.

    Correct your imports by:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*; // <- Here
    

    By the way, you should avoid global imports, and just import what you want like this:

    import javax.swing.JOptionPane;