呵呵,我能力以外,初学,有些地方看不懂。>_<

解决方案 »

  1.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class KeyTest
    {
    public static void main( String args[] )
    {
    KeyFrame frame=new KeyFrame();
    //设置退出进程的windowlistener
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
     });
    frame.show();

    }
    }
    //定义框架类
    class KeyFrame extends JFrame
    {
    //在框架类的constructor里设置框架属性,并且加载Panel,在 Panel上装按钮一个
    public KeyFrame()
    {
    setBounds(100,100,400,400);
    setTitle("KeyTest");
    Container container=getContentPane();
    JPanel bPanel=new JPanel();
    bPanel.setFocusable(false);
    container.add(bPanel);
    ColorAction ca=new ColorAction(Color.yellow,"Yellow");
    JButton but=new JButton(ca);
    bPanel.add(but);
    }
    //这是按钮对象关联的Action类定义
    class ColorAction extends AbstractAction
    {
    public ColorAction(Color color,String string)
    {
    putValue("color",color);
    putValue(NAME,string);
    }
    //actionPerformed意思是Action触发后,改变背景颜色至黄色
    public void actionPerformed(ActionEvent ae)
    {
    Color bgc=(Color)getValue("color");
    setBackground(bgc);
    repaint();
    }
    }
    }各位哥哥姐姐,已经注释了,满意了嘛:)
      

  2.   

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;public class KeyTest
    {
    public static void main( String args[] )
    {
    KeyFrame frame=new KeyFrame();
    frame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
     });
    frame.show();

    }
    }class KeyFrame extends JFrame
    {
    JPanel bPanel=new JPanel();
    public KeyFrame()
    {
    setBounds(100,100,400,400);
    setTitle("KeyTest");
    Container container=getContentPane(); bPanel.setFocusable(false);
    container.add(bPanel);
    ColorAction ca=new ColorAction(Color.yellow,"Yellow");
    JButton but=new JButton(ca);
    bPanel.add(but);


    }

    class ColorAction extends AbstractAction
    {
    public ColorAction(Color color,String string)
    {
    putValue("color",color);
    putValue(NAME,string);
    }

    public void actionPerformed(ActionEvent ae)
    {
    Color bgc=(Color)getValue("color");
    bPanel.setBackground(bgc);
    repaint();
    }
    }
    }