lefty
lefty

Reputation: 25

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

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)

Upvotes: 0

Views: 386

Answers (1)

Martin Carpella
Martin Carpella

Reputation: 12603

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

Upvotes: 1

Related Questions