O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

There's more...

Let's create a script to create a simple Swing GUI with a button and an event handler for the button. We will also look at how we leverage imports and implement interfaces using an anonymous inner class approach.

First, we'll create a new JavaImporter object with the required Java packages:

var javaGui = new JavaImporter(javax.swing, java.awt, java.awt.event);

We use the with(obj){} clause to wrap all the statements using the required imports:

with(javaGui){  //other statements}

Next, we create JButton and provide ActionListener to listen to its click events:

var button = new JButton("My Button");button.addActionListener(new ActionListener({  actionPerformed: function(e){    print("Button clicked");  }}));

Then, we create JFrame ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required