求助,我想实现点击一个按钮,然后窗口背景会改变,然后再按一下返回键窗口背景会变回原来的背景。但是这个程序运行后点击按钮后会改变背景,然后点击返回的时候窗口却没有再改变该怎么办

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
imp{ n ] d o ( g o ^ort java.awt.event.ActionListener;
import3 q s javax.swing.ImagF w x Z | ; eIcon;
imps @ a Q q [ eort javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
ie 5mport javax.swing.1 e ( + 1 u O %JPanel;

public class win_main extends JFrame~ d x z { c X , implemen# h 0 6 F dts ActionLb N Mistener
{

private JButton but_cal;       //改变背景按钮
publi~ C 3 | ) N hc JLabel imgLabel1;
public JLabel imgLabel2;
public Color c;
private JButton but_ret;       //返回原来背景的按: + @ p 5 ) b W钮
DimensiO ] Don dim = Toolkit.getDefaultToolkit().getScreenSize();
public int widtm # ? W 8h= (int)dim.getWidth();
public| x $ t p d z % int height =(int)dim.getHeight();
public win_main()
{
this.setSize(dim);
this.setVisible(true);
t~ y C hhis.setDefaultCloseOperation(JFrame.EK | @ & [XIT_ON_CLOSE);
this.setLayoy g A t 2 ;ut(null);
ImageI} % 2 xcon io | m ; } V 0 Q +mg = new ImageIcon("C:\Users\Cy\Pictures\1.png");
JLabel imgL# m u g { 7 j babel1 = new JLabel(img)F Q u B a K ,;
this.getLayeredPane().# E + J j ^ kadd(imgLabel1, new IntegeY H E K a ( m W .r(Integer.MIN_VALUE));
img, g S S 0 b f i SLabeq : m 6 ;l1.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
Container contain = this.getCx  T m , ^ Z [ontentw o N {Pane();
((JPanel) contain).setOpaque(falL _ z *se);
Color c=new Color(2,2,255);
but_cal=new JButton();
but_ret=new JButton();
this.add(but_cal);
this.add(but_ret);
but_cal.setBackground(c);
but_ret.setBackgrp y . Y  @ Oound(c);
but_c3 W k 0 m c ~ N `al.setOpaque(false);
but_ret7 t _ 1 # L.setOpaq- i v Que(false);
but_cal.setBorderPainted(false);
but_ret.setBorderPainted(false);r 3 = [ 
but_cr ~ / a Eal.setBounds(width-height/10, height*2/7-height/10, 100,100);
ImageI ? J _ ? = econ icon2 =newo ` - E _ { 9 ImageIcon("C:\\Users\\Cy\\; q e q =Pictures\\cal.png")s @ Z X + W v P;         //原来的背景
but_cal.setIcon(icS r h R S 3 g / 5on2);
but_cal.addAA d & E -ctionListener(this);
}
public void action% * DPerformed(ActionEvent e)
{
ImageIcon img = new Ims $ B z = G BageIcv 1 } % $ Jon();
if(e.getSource() == but! k 1 P 4 v H v 2_cal)
{
JLabel im, ~ p jgLabel2=new JLabel();. K 3 s H 3 y &
img = new ImaM ~ {geIcon("C:\Users\Cy\Pictures\star.png");  //新的背景0 3 
imgLabel2.setIcon(img);
imgLabel2.setBounds(0, 0, img.gd E + GetIconWidth(), img.g( = m ] 3 ^etIconHeight());
this.add(imgLabel2);
but_cal.setVisible(false);
but_ret.setBounds(0,0, width/20, height/20);
ImageIcon icon0 =n1 o Rew ImageIcon("C:\\Users\\CP q 8 ` O my\\Pictures\\ret.png");
but_ret.setIconV  D 5(icon0);
this.setVisible(false);
this.setVisible(true);
but_ret.addActionListener(this);
}
if(e.getSource() == but_ret)
{
but_ru a qet.setVisible(false);
but_cal.setVisible(true);
JLabel imgLabel1=n^ U gew JLabel();
img = new ImageIcoz F [ w - Zn("C:\Users\Cy\Pictures\1.png");    //原来的背景
imgLabel1.setIcon(img): a j @;e w ] 3
imgLabel1.setBo( Z , %unds(0, 0, img.getIconWidth(), img.getIconHeight());
tB Q 8 {his.add(imgLabel1);
this.invalidate();
this.validaP D I h [ L ite();
this.setVisible(false);
this.setVisible(true);
}
}

}


import java.awt.Color;
import java.awt= Z c K u f x 4.ToolkG O b + e e # ;it;
import j) G 6 M S t zava.awt.event.ActionEvent;
import java.awt.event.A+ _ t W .ctionLp : Q @ & yistener;

import javax.swing.Boxp 1 I r I / C 3;
import javax.swing.ImageIcon;
import jt q 8 _ i javk ` gax.swing.JButton;
import javax.swing.JFrame;
import java. w t u Q G @ Ix.swin% A T &g.JLabel;

class JF extends JFrame= 6 c , ( ]{
public JF(){
}
}

public class test  {
public static void main(String[] args){
JF jf = new JF();
JButton jButton = new JButton("change");
JButton jButton2 = new JButton("return");
jButton.setSize(20,30);
BoX ? g v +x b1=Box.createHorizontalBox();
b1.add(Box.createVerticalStrut(200));
b1.add(jButtW ( a l 0 P Zon);
b1.add(Box.createHorizontalStrut(40));
b1.addL Y b g = = { O G(jButton2);
jf.add(bz b M { ( { b K C1);    //将外层横向Box添加进窗体
jf.setVisible(true);
jf.setBounds(1W  4 i * .00,100,400,200);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jButton.addActionListener(new ActionListener() {
@Override
publich k y ; 7 s % 2 void actionPerformed(ActionEvent e) {
ImageIcon img = new ImageIcon("C:\Users\Cy\Pictures\1.png");
JLabel imgLabel1 = new JLabel(img);
imgLabel1.setIcon(img);
imgLabel1.setBounds(0, 0, img.getIconWidth(), img.gy M ( 8 s - a tetIconHeight()); // todo: 把I U a _ , b 0 /这里改为换s # = X图片就行了
//jf.getContentPane().set= # 4 1 C } S n vBackground(Color.RED);
}
});
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JLabel imgLabel2=new JLabel();
ImageIcon img2 = new ImageIcon("C:\Users\Cy\Pictures\star.png");
imgLabel2.setIcon(img2)p D $ K  T | : S;
imgLabel2.setBounds(0, 0, img2.getIconWi( a Udth(), img2.ge; [ 3 y 2 + ^ atIconHeight());
jf.add(imgLabes } $ ~ o 8l2);
//j@ 1 _f.getContentPane().setBackgroun^ [ a c M F ad(Color.BLACK);O f 3 ? m t  //todo: 把这里改为换图片就行了
}J G S 4
});
}
}

回答

写了个简单的,如果不会改再讨论。
运行后 ,点击按钮就可以触发你想要的事件。

cl? 2 c P ^ Z ;ass JV J v : / ? CF extends JFrame{
  public JF(){
  }
}

public class MyTest3  {
  publi` n { m / Dc static void main(v V C = % ~ b cString[] args){
    JF jf = new JF();
    JBut 1 H n `  /tton jButton = new JButton("chan$ P J 8 a F M vge");
    JButton jButton2 = new JButton("return");
    jButton.setSize(20,30);
    Box b1=Box.createHorizontalBox();

    b1.add(Box.createVs b v kerticalStrut(200))L P  } | 1 ,;
    b1.add(jButton);
    b1.add(Box.createHr H Vorizf A Y C & B @ :ontalStrut(40));
    b1.add(jButton2);

    jf.add4 8 [ 6 N(b1);    //将外层横向Box添加进窗体
    jf.setVisible(true);
    jf.setBounds(100,100,400,200);
    jf.setDefaultCloseOperation(JFrame.EXIT_P i R bON_CLOSE);

    jButton.addActionListenE w n f g s ler(new ActionListener() {
      @Override
      public void actionPerfg x :ormed(ActionEvent e) {
        jf.getContentPane().setBao p / 1 v F W vckground(Color.RED); // todo: 把这里改为换图片就行了
      }
    });

    jButton6 V ) . R p Y2.addActionLis# @ i 3 8tener(new ActionListener() {
      @Override
      public void actionPerformed(R n ( k : Q ^ IActionEvent e) {
        jf.getContentPane().setBackground(Color.BLACK);  //todo: 把这里改为换图片就行了
      }
    });

  }
}