Explanation of Solution
 Modified code:
//import required header files
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//definition of "ColorDemo" class
public class ColorDemo extends JApplet implements ActionListener
{
    //declare the required variables
    public static final int WIDTH = 400;
    public static final int HEIGHT = 300;
    public static final int NUMBER_OF_CHAR = 20;
    private JTextField colorName;
    //definition of "init()" method
    public void init()
    {
        //create content pane
        Container contentPane = getContentPane( );
        //set the background color
        contentPane.setBackground(Color.GRAY);
        //set the layout
        contentPane.setLayout(new FlowLayout( ));
        //create a button
        JButton showButton = new JButton("Show Color");
        //add the action listener to the button
        showButton.addActionListener(this);
        //add a button
        contentPane.add(showButton);
        //create a text field
        colorName = new JTextField(NUMBER_OF_CHAR);
        //add a text field
        contentPane.add(colorName);
    }
    //definition of "actionPerformed" method
    public void actionPerformed(ActionEvent e)
    {
        //create content pane
        Container contentPane = getContentPane( );
        //try block
        try
        {
            //set the background color
            contentPane.setBackground(
            getColor(colorName.getText()));
        }
        //catch block
        catch(UnknownColorException exception)
        {
            //set a text
            colorName...
Want to see the full answer?
Check out a sample textbook solutionChapter 9 Solutions
Java: An Introduction to Problem Solving and Programming (7th Edition)
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education