Exercise Nine 

Exercise Nine
  • Okay, let's make our simple example from last exercise handle events using the 1.1 methodology: 

  • [Example]

    import java.awt.*;
    import java.awt.event.*;
    
    public class Event11Example extends Panel 
           implements ActionListener
      {
      public Panel     displayPanel;
      public Button    colorButton;
      public Color[]   colors = {Color.black, 
                                 Color.red, 
                                 Color.yellow,
                                 Color.green};
      private int      counter = 0;
    
      public Event11Example()
        {
        setLayout(new BorderLayout());
        displayPanel = new Panel();
        add("Center", displayPanel);
        colorButton = new Button("Change Color");
        colorButton.addActionListener(this);
        add("South", colorButton);
        }
    
      public static void main(String[] args)
        {
        Frame baseFrame = new Frame();
        Event11Example ee = new Event11Example();
        baseFrame.add("Center", ee);
        baseFrame.reshape(10,10,200,200);
        baseFrame.setTitle("Event Example");
        baseFrame.show();
        }
    
      public void actionPerformed(ActionEvent evt)
        {
        displayPanel.setBackground(colors[counter]);
        if (counter <=2)     
           counter++;
        else
           counter =0;
        }
      }
     

Additional Resources:

Beans Event Listeners
Table of Contents
Adapters


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory