The Java Shell tool (JShell) is an interactive tool for learning the Java programming language and prototyping Java code. JShell is a Read-Evaluate-Print Loop (REPL), which evaluates declarations, statements, and expressions as they are entered and immediately shows the results. The tool is run from the command line. JShell was introduced in java 9 and hence can not be used in java 8.
Jshell has reduced all the efforts that are required to run a Java program and test business logic. If we don’t use Jshell, creating of Java program involves the following steps.
Using the concept of Swing class for dealing with the frames one can create a frame in JShell and can add a Button on the Frame and works the same as ActionListener in Swing.
- Firstly import awt package for creating a Frame
- Create Frame Object
- Set Visibility
- Set Size of the Frame
- Set Location of the Frame
- Set Background Color of the Frame
- Create a Button
- Add Button to the Frame
- Set the Frame Layout
- Use ActionListener
Step 1: Firstly import awt package for creating a Frame
jshell>import java.awt.* ;
Step 2: Creating Frame Object
jshell>Frame f = new frame() ; || 'f' is object name
f==>java.awt.Frame[frame, 0, 0, 0, 0*0, invalid, hidden, layout...t, title = resizable, normal ]
Step 3: Setting Visibility for a frame as after creating a Frame object, the frame is still not visible on the screen.
Syntax: Setting Visibility to true
Output: When the visibility of the frame is true.
- By default, Visibility of the Frame is False.
- This Frame is displayed after setting Visibility “True”.
- This Frame will be visible on top left corner.
- If frame visibility is set to “False”, then frame will be hidden.
Syntax: Setting Visibility to false
Output: When the visibility of the frame is false.
Step 4: Setting Size of the Frame
Syntax: Here the first parameter is height and the second is the width in setSize() function.
jshell>f.setSize(length_pixelValue , breadth_pixelValue) ;
Arbitral considering values:
- length_pixelValue = 500
- breadth_pixelValue = 500
jshell>f.setSize(500, 500) ;
Output: This Frame will be visible in top left corner.
Step 5: Setting Location of the Frame
- Setting Location of Frame refers to the distance from the X and Y axis.
- The first parameter defines the distance from the left margin of the screen.
- Second Parameter r defines the distance from the top margin of the screen.
Step 6: Setting Background Color of the Frame
Step 7: Creating a Button
jshell> Button b1=new Button(“Click!!”)
b1 ==> java.awt.Button[button0,0,0,0×0,invalid,label=Click!!]
Step 8: Adding Button to the Frame
$8 ==> java.awt.Button[button0,0,0,0×0,invalid,label=Click!!]
Step 9: Setting the Frame Layout
jshell> f.setLayout(new FlowLayout());
Step 10: Using ActionListener
On Clicking on the button the background color is changed to Cyan. Here we can also say that in JShell we can use Lambda Expression. Hence, successfully able to change the background color.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.