Changing Background Color Using ActionListener in JShell in Java
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.
Procedure:
- 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
Implementation:
Step 1: Firstly import awt package for creating a Frame
Syntax:
jshell>import java.awt.* ;
Step 2: Creating Frame Object
Syntax:
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
jshell>f.setVisible(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
jshell>f.setVisible(false) ;
Output: When the visibility of the frame is false.
No image
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
Syntax:
jshell>f.setLocation(100,100) ;
- 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
Syntax:
jshell>f.setBackground(Color.yellow) ;
Output:
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
jshell> f.add(b1)
$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
Syntax:
jshell>b1.addActionListener(e->setBackground(Colour.cyan)) ;
Output:
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.
Example:
Java
// Java Program to change Background Color // Using ActionListener in JShell // Importing awt package jshell > import java.awt.* // Creating a frame object jshell > Frame f = new Frame() // Setting object created above f == > java.awt .Frame[frame0, 0 , 0 , 0x0 , invalid, hidden, layo... t, title =, resizable, normal] // Setting visibility to frame jshell > f.setVisible( true ) // Setting size of frame jshell > f.setSize( 500 , 500 ) // Setting location of frame from X and Y axis jshell > f.setLocation( 100 , 100 ) // Setting background color jshell > f.setBackground(Color.yellow) jshell > Button b1 = new Button( "Click!!" ) b1 == > java.awt.Button[button0, 0 , 0 , 0x0 , invalid, label = Click !!] jshell > f.add(b1) $ 8 == > java.awt.Button[button0, 0 , 0 , 0x0 , invalid, label = Click !!] jshell > f.setLayout( new FlowLayout()); // Changing background color jshell > b1.addActionListener(e -> f.setBackground(Color.cyan)) // Terminate jshell > |
Please Login to comment...