java新手的一个新问题:
我也知道,每一个类都有他的方法:
比如:类A有一个set()方法,我们一般的做法是:用A构造一个对象a,然后调用a.set()现在有如下一段代码:(《core java》关于swing的图形程序设计)
这是一个整段的代码,放在ide中就可以运行的:/**
   @version 1.32 2004-05-04
   @author Cay Horstmann
*/import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ButtonTest
{  
   public static void main(String[] args)
   {  
      ButtonFrame frame = new ButtonFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
}/**
   A frame with a button panel
*/
class ButtonFrame extends JFrame
{
   public ButtonFrame()
   {
      setTitle("ButtonTest");
      setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);      // add panel to frame      ButtonPanel panel = new ButtonPanel();
      add(panel);
   }   public static final int DEFAULT_WIDTH = 300;
   public static final int DEFAULT_HEIGHT = 200;  
}/**
   A panel with three buttons.
*/
class ButtonPanel extends JPanel
{  
   public ButtonPanel()
   {  
      // create buttons      JButton yellowButton = new JButton("Yellow");
      JButton blueButton = new JButton("Blue");
      JButton redButton = new JButton("Red");      // add buttons to panel      add(yellowButton);
      add(blueButton);
      add(redButton);      // create button actions      ColorAction yellowAction = new ColorAction(Color.YELLOW);
      ColorAction blueAction = new ColorAction(Color.BLUE);
      ColorAction redAction = new ColorAction(Color.RED);      // associate actions with buttons      yellowButton.addActionListener(yellowAction);
      blueButton.addActionListener(blueAction);
      redButton.addActionListener(redAction);
   }   /**
      An action listener that sets the panel's background color. 
   */
   private class ColorAction implements ActionListener
   {  
      public ColorAction(Color c)
      {  
         backgroundColor = c;
      }      public void actionPerformed(ActionEvent event)
      {  
         setBackground(backgroundColor);
      }      private Color backgroundColor;
   }
}我想知道,以下这段代码:
private class ColorAction implements ActionListener
   {  
      public ColorAction(Color c)
      {  
         backgroundColor = c;
      }      public void actionPerformed(ActionEvent event)
      {  
         setBackground(backgroundColor);
      }      private Color backgroundColor;
   }
这句setBackground(),是谁来执行这个函数啊,(怎么感觉像是面向过程的设计方法啊?)对象的参数是怎么传进来的呢??编译器又是怎么通过编译的呢??

解决方案 »

  1.   

    (呵呵,分数太少了。)
    你把这个监听器类的三个实例已经注册给你的三个Button了,
    所以当你点击Button的时候,就会有一个事件传递给actionPerformed(ActionEvent event)
    接着就执行了。
      

  2.   

    Button类有setbackground()这个方法吗?
    注:这个监听器类是一个内部类
      

  3.   

    public interface ActionListener
    extends EventListener
    The listener interface for receiving action events. The class that is interested in processing an action event implements this interface, and the object created with that class is registered with a component, using the component's addActionListener method. When the action event occurs, that object's actionPerformed method is invoked.要常翻SDK Documentation啊