Page 712 - Introduction to Programming with Java: A Problem Solving Approach
P. 712

                678 Chapter 16 GUI Programming Basics
 //*******************************************************
public ColorChooser()
{
setTitle("Background Color Chooser");
setSize(WIDTH, HEIGHT);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
// end ColorChooser constructor
//*******************************************************
}
private void createContents()
 {
 grayButton = new JButton("Gray");
grayButton.setBackground(Color.LIGHT_GRAY);
grayButton.addActionListener(new ButtonListener());
add(grayButton);
blueButton = new JButton("Blue");
blueButton.setBackground(new Color(135,206,250));
        Apago PDF Enhancer
     }
blueButton.addActionListener(new ButtonListener());
add(blueButton);
// end createContents
//*************************************************************
// Inner class for event handling.
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
Container contentPane = getContentPane();
if (e.getSource() == grayButton)
{
// Change the window background color to gray.
contentPane.setBackground(Color.GRAY);
 }
  else
These lines change the window’s background color.
 }
// end class ButtonListener
}
// end actionPerformed
{
}
// Change the window background color to blue.
contentPane.setBackground(Color.BLUE);
This sets the Gray button’s color.
 This sets the Blue button’s color.
Figure 16.14b ColorChooser program—part B
















































   710   711   712   713   714