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...

You do need to use an absolute cell reference when copying formulas with named ranges

Indicate whether the statement is true or false

Computer Science & Information Technology

C# allows you to access the individual characters in a string using ____________.

a. switch statements b. subscript notation c. named constants d. character literals

Computer Science & Information Technology