Rewrite the following code to use a Lambda expression for the button action listener.

What will be an ideal response?


Rewrite the following code to use a Lambda expression for the button action listener.
```
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

public class ButtonDemo3 extends JFrame
{
public ButtonDemo3( )
{
setSize(250, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Button Demo");
Container contentPane = getContentPane( );
contentPane.setBackground(Color.BLUE);
contentPane.setLayout(new FlowLayout( ));

JButton goButton = new JButton("Green");
goButton.addActionListener(
{ e } -> contentPane.setBackground(Color.GREEN));
contentPane.add(goButton);
}

public static void main(String[] args)
{
ButtonDemo3 buttonGui = new ButtonDemo3( );
buttonGui.setVisible(true);
}
}

Answer:

import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;

public class ButtonDemo3 extends JFrame
{
public ButtonDemo3( )
{
setSize(250, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Button Demo");
Container contentPane = getContentPane( );
contentPane.setBackground(Color.BLUE);
contentPane.setLayout(new FlowLayout( ));

JButton goButton = new JButton("Green");
goButton.addActionListener(
e -> contentPane.setBackground(Color.GREEN));
contentPane.add(goButton);
}

public static void main(String[] args)
{
ButtonDemo3 buttonGui = new ButtonDemo3( );
buttonGui.setVisible(true);
}
}
```

Computer Science & Information Technology

You might also like to view...

It is possible to use POD to print one copy of a book

Indicate whether the statement is true or false

Computer Science & Information Technology

If information to a linked file is changed, this information can be updated in your already open Word document by right-clicking the link, and then clicking ________

Fill in the blank(s) with correct word

Computer Science & Information Technology

To include calculated fields in queries, enter a name for the calculated field, a(n) _____, and then the expression in one of the columns in the Field row.

A. colon (:) B. semi-colon (:) C. equal sign (=) D. exclamation point (!)

Computer Science & Information Technology

Cisco controllers support a maximum of ________ WLANs, but only ________ of them can be actively configured on an AP.

A) 16, 5 B) 512, 16 C) 100, 50 D) 100, 5

Computer Science & Information Technology