What's wrong with this use of EventQueue.invokeLater?

357 Views Asked by At

So I was trying to figure out how this snake game works:

http://zetcode.com/tutorials/javagamestutorial/snake/

When I copied the code into a compiler, I got many errors in this one block of code:

public static void main(String[] args) {

     EventQueue.invokeLater(() -> {
         JFrame ex = new Snake();
         ex.setVisible(true);
     });
  }
}

Here are the errors: (Line 27 is the EventQueue.invokeLater line)

Error: illegal start of expression (Line 27)
Error: illegal start of expression (Line 27)
Error: illegal start of expression (Line 27)
Error: ';' expected (Line 27)
Error: illegal start of type (Line 30)
Error: class, interface, or enum expected (Line 32)
1

There are 1 best solutions below

3
Martin C. On

You need to have at least Java 8 for using Lambdas (like you do in this example).

For Java 7, you'll need to resort to resort to using Runnable instead of Lambda

EventQueue.invokeLater(new Runnable() {
    public void run() {
      JFrame ex = new Snake();
      ex.setVisible(true);
    }
});