Java swing error dialog

I have a dialog to display but it gives compilation errors. Compilation errors are listed in the last section.

import javax.swing.*;

class SwingDemo {
    SwingDemo() {
        JFrame jfrm = new JFrame("A Simple Swing Application");
        jfrm.setSize(275, 100);
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JLabel jlab = new JLabel(" Swing means powerful GUIs.");
        jfrm.add(jlab);
        jfrm.setVisible(true);
    }

    public static void main(String args[]) {
        public void run() {
            new SwingDemo();
        }
    }
}

      

Errors:

Multiple markers at this line
    - Syntax error on token "void", @ expected
    - Syntax error, insert "enum Identifier" to complete EnumHeaderName
    - Syntax error, insert "EnumBody" to complete BlockStatements

      

+3


source to share


2 answers


Just replace your main function with this.



public static void main(String args[]) {
    // Create the frame on the event dispatching thread.
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new SwingDemo();
        }
    });
}

      

+6


source


First of all, are you using an IDE?

Your run () method is inside your main () method. You don't need a startup method anyway. Just create an instance from main () of the new SwingDemo (); and remove the run () function like this:



 public static void main(String[] args) {
         new SwingDemo();
 }

      

0


source







All Articles