class  AllPanels  extends  JPanel  implements  ActionListener  
{  
           AllSwings  frame;  
           JButton  b1=new  JButton("White");  
           JButton  b2=new  JButton("Black");  
           JButton  b3=new  JButton("Green");  
           JButton  b4=new  JButton("Blue");  
           AllPanels()  
           {  
                       setLayout(new  GridLayout(2,2,5,5));  
                       add(b1);  b1.addActionListener(this);
                       add(b2);  b2.addActionListener(this);
                       add(b3);  b3.addActionListener(this);
                       add(b4);  b4.addActionListener(this);           }  
           public  void  actionPerformed(ActionEvent  evt)  
           {  
                       AllSwings  als=new  AllSwings();  
                       Object  source=evt.getSource();  
                       if(source==b1)  
                       {  
                                   als.swatch.setBackground(Color.white);  
                       }  
                       else  if(source==b2)  
                       {  
                                   als.swatch.setBackground(Color.black);  
                       }  
                       else  if(source==b3)  
                       {  
                                   als.swatch.setBackground(Color.green);  
                       }  
                       else  if(source==b4)  
                       {  
                                   als.swatch.setBackground(Color.blue);  
                       }  
                       repaint();  
           }  
}

解决方案 »

  1.   

    import  java.awt.*;  
    import  java.awt.event.*;  
    import  javax.swing.*;  
     
    public  class  AllSwings  extends  JFrame  
    {   class  AllPanels  extends  JPanel  implements  ActionListener  
    {  
               //AllSwings  frame;  
               JButton  b1=new  JButton("White");  
               JButton  b2=new  JButton("Black");  
               JButton  b3=new  JButton("Green");  
               JButton  b4=new  JButton("Blue");  
               AllPanels()  
               {  
                           setLayout(new  GridLayout(2,2,5,5));  
                           add(b1);  b1.addActionListener(this);
                           add(b2);  b2.addActionListener(this);
                           add(b3);  b3.addActionListener(this);
                           add(b4);  b4.addActionListener(this);

               }  
               public  void  actionPerformed(ActionEvent  evt)  
               {  
                           //AllSwings  als=new  AllSwings();  
                           Object  source=evt.getSource();
                           if(source==b1)  
                           {  
                               
                                       swatch.setBackground(Color.white);  
                           }  
                           else  if(source==b2)  
                           {  
                                       swatch.setBackground(Color.black);  
                           }  
                           else  if(source==b3)  
                           {  
                                       swatch.setBackground(Color.green);  
                           }  
                           else  if(source==b4)  
                           {  
                                       swatch.setBackground(Color.blue);  
                           }  
                           repaint();  
               }  
    }           AllPanels  pls;  
               JPanel  swatch;  
               AllSwings()  
               {  
                           setTitle("AllSwings");  
                           setSize(300,100);  
                             
                           JPanel  pane=new  JPanel();  
                           pane.setLayout(new  GridLayout(1,2,5,10));  
                             
                           swatch=new  JPanel();  
                           swatch.setBackground(Color.black);  
                             
                           pls=new  AllPanels();  
                           pane.add(swatch);  
                           pane.add(pls);  
                             
                           setContentPane(pane);                          
               }  
               public  static  void  main(String[]  args)  
               {  
                           AllSwings  as=new  AllSwings();  
                           as.setResizable(false);  
                           as.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                           as.show();  
               }  
    }  
    把AllPanels变成内部类倒是挺简单的,但是方法不太好,呵呵!