import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Third
{
 public static void main(String[] args)
 {
  MyFrame frame=new MyFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.show();
 }
}
class MyFrame extends JFrame
 {
  public MyFrame()
   {
    setSize(800,800);
    setTitle("MyAction");
    Container con=getContentPane();
    MyPanel panel=new MyPanel();
    con.add(panel);
   }
  }
 class MyPanel extends JPanel
 {
  public MyPanel()
  {
   JButton yellowButton=new JButton("Yellow");
   JButton blueButton=new JButton(new ImageIcon("arg.gif"));
   JButton redButton=new JButton("Red");
   add(yellowButton);
   add(blueButton);
   add(redButton);
   ColorAction yellowAction=new ColorAction(Color.YELLOW);
   ColorAction blueAction=new ColorAction(Color.BLUE);
   ColorAction redAction=new ColorAction(Color.RED);
   yellowButton.addActionListener(yellowAction);
   blueButton.addActionListener(blueAction);
   redButton.addActionListener(redAction);
  }
 }
 class ColorAction implements ActionListener
{
  public ColorAction(Color c,);
  {
   cor=c;
  }
  public void actionPerformed(ActionEvent event)
   {
    MyPanel panel=new MyPanel();
    panel.setBackground(cor);
   }
  private Color cor; 
}这个程序的问题是破坏了ColorAction的封装吧?要实现同样的目标应该怎样修改此程序呢?谢谢!

解决方案 »

  1.   

    public ColorAction(Color c,);
      {
       cor=c;
      }---------------------------
    这边不对吧
      

  2.   

    public ColorAction(Color c,);   // 是你这里的书写问题吗?
      {
       cor=c;
      }-------------------
    (Color c,) ??  应该是  (Color ,c)
      

  3.   

    纠正 下
    应该是 (Color c)
      

  4.   

    你的代码我运行了 可能是
      ColorAction yellowAction=new ColorAction(Color.YELLOW);
       ColorAction blueAction=new ColorAction(Color.BLUE);
       ColorAction redAction=new ColorAction(Color.RED);
       yellowButton.addActionListener(yellowAction);
       blueButton.addActionListener(blueAction);
       redButton.addActionListener(redAction);
    的问题!!我看看要是有时间,做一个和你同功能的,给你贴上来
      

  5.   

    public ColorAction(Color c)
      {
       cor=c;
      }
     我是初学者,我已经改了,结果能运行,但按了按钮后却没反应,应该是破坏了封装了啊。怎么搞的,哪位高手帮帮忙啊?
      

  6.   

    //楼主的程序看了真不爽import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Third
    {
     public static void main(String[] args)
     {
      MyFrame frame=new MyFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.show();
     }
    }
    class MyFrame extends JFrame
     {
      public MyFrame()
       {
        setSize(800,800);
        setTitle("MyAction");
        Container con=getContentPane();
        MyPanel panel=new MyPanel();
        con.add(panel);
       }
      }
     class MyPanel extends JPanel
     {
      public MyPanel()
      {
       JButton yellowButton=new JButton("Yellow");
       JButton blueButton=new JButton(new ImageIcon("arg.gif"));
       JButton redButton=new JButton("Red");
       add(yellowButton);
       add(blueButton);
       add(redButton);
       ColorAction yellowAction=new ColorAction(this,Color.YELLOW);
       ColorAction blueAction=new ColorAction(this,Color.BLUE);
       ColorAction redAction=new ColorAction(this,Color.RED);
       yellowButton.addActionListener(yellowAction);
       blueButton.addActionListener(blueAction);
       redButton.addActionListener(redAction);
      }
     }
     class ColorAction implements ActionListener
    {
      public ColorAction(MyPanel p,Color c)
      {
       cor=c;
       parent = p;
      }
      public void actionPerformed(ActionEvent event)
       {
        //MyPanel panel=new MyPanel();
        //panel.setBackground(cor);
      parent.setBackground(cor);
       }
      private Color cor; 
      private MyPanel parent;
    }
      

  7.   

    给你参考一下我写的吧
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class ChangeColor extends JFrame{
    private Panel2 panel;
    public ChangeColor(){
    panel = new Panel2();

    Container c = getContentPane();
    c.setLayout(new BorderLayout());
    c.add(panel,BorderLayout.CENTER);

    this.setSize(new Dimension(400,300));
    this.setLocation(200,200);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public static void main(String args[]){
    new ChangeColor().setVisible(true);
    }}class Panel2 extends JPanel implements ActionListener{
    private JButton yellow,blue,red;

    public Panel2(){
    yellow = new JButton("yellow");
    blue = new JButton("blue");
    red = new JButton("red");

    yellow.addActionListener(this);
    blue.addActionListener(this);
    red.addActionListener(this);

    this.setLayout(new FlowLayout());
    this.add(yellow);
    this.add(blue);
    this.add(red);
    }

    public void actionPerformed(ActionEvent e){
    if(e.getSource() == yellow){
    this.setBackground(Color.YELLOW);
    }
    else if(e.getSource() == blue){
    this.setBackground(Color.BLUE);
    }
    else if(e.getSource() == red){
    this.setBackground(Color.RED);
    }
    }}