简体   繁体   中英

java swing error dialog box

I have a dialog box to be shown but it is giving compilation errors. The compilation errors are given in the last part.

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();
        }
    }
}

The errors are:

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

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();
        }
    });
}

First of all, do you use an IDE?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM