当然不起作用了。当初得到的颜色originalcolor,不管是什么,当你按下restorebtn时,又被你设为背景色了。本来就是背景色,你又弄了一边,看起来当然没有效果。

解决方案 »

  1.   

    furarmy,不是你说的意思:比如:我先按下了第一个按钮,背景变成黄色;我接着按restore按钮时也不起作用!
      

  2.   

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    class  MyPanel  extends  JPanel  implements  ActionListener 

    private  JButton  yellowbtn; 
    private  JButton  bluebtn; 
    private  JButton  redbtn; 
    private  JButton  restorebtn; 
    Color  originalcolor  =  getBackground(); public  MyPanel() 

    yellowbtn  =  new  JButton("Yellow"); 
    bluebtn  =  new  JButton("Blue"); 
    redbtn  =  new  JButton("Red"); 
    restorebtn  =  new  JButton("Restore"); add(yellowbtn); 
    add(bluebtn); 
    add(redbtn); 
    add(restorebtn); yellowbtn.addActionListener(this); 
    bluebtn.addActionListener(this); 
    redbtn.addActionListener(this); 
    restorebtn.addActionListener(this); 
    } public  void  actionPerformed(ActionEvent  evt) 

        Object  evtSource  =  evt.getSource();
        Color  color  =  getBackground(); 
        if  (  evtSource  ==  yellowbtn  )  color  =  Color.yellow; 
            else    if  (  evtSource  ==  bluebtn  )  color  =  Color.blue; 
                              else    if  (  evtSource  ==  redbtn  )  color  =  Color.red; 
            else    if  (  evtSource  ==  restorebtn  )  color  =  originalcolor; 
            setBackground(color); 
            repaint(); } 
    public static void main(String[] args){
            JDialog.setDefaultLookAndFeelDecorated(true);
            JFrame.setDefaultLookAndFeelDecorated(true);
            Toolkit.getDefaultToolkit().setDynamicLayout(true);
            System.setProperty("sun.awt.noerasebackground","true");        try {
                javax.swing.plaf.metal.MetalLookAndFeel.setCurrentTheme( new javax.swing.plaf.metal.DefaultMetalTheme());
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
            }  
            catch ( UnsupportedLookAndFeelException e ) {
                System.out.println ("Metal Look & Feel not supported on this platform. \nProgram Terminated");
                System.exit(0);
            }
            catch ( IllegalAccessException e ) {
                System.out.println ("Metal Look & Feel could not be accessed. \nProgram Terminated");
                System.exit(0);
            }
            catch ( ClassNotFoundException e ) {
                System.out.println ("Metal Look & Feel could not found. \nProgram Terminated");
                System.exit(0);
            }   
            catch ( InstantiationException e ) {
                System.out.println ("Metal Look & Feel could not be instantiated. \nProgram Terminated");
                System.exit(0);
            }
            catch ( Exception e ) {
                System.out.println ("Unexpected error. \nProgram Terminated");
                e.printStackTrace();
                System.exit(0);
            }   JFrame jf=new JFrame("arron");
       jf.setContentPane(new MyPanel());
       jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       jf.setLocation(200,200);
       jf.pack();
       jf.show();}
    }
    j2sdk1.4.0-rc下调试通过,实现你所说的。
    你的restore是这样的,每选一种色,就使originalcolor变为该色,当你按restore时当然还是使用当前色啦!
      

  3.   

    我知道了,昨天晚上下楼时突然省悟过来了,应该把
    originalcolor = getBackground();放在构造函数中。谢谢两位!