CY9117207934 2020-07-14 18:02 采纳率: 50%
浏览 104
已采纳

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

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class win_main extends JFrame implements ActionListener
{

private JButton but_cal;       //改变背景按钮
public JLabel imgLabel1;
public JLabel imgLabel2;
public Color c;
private JButton but_ret;       //返回原来背景的按钮
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
public int width= (int)dim.getWidth();
public int height =(int)dim.getHeight();
public win_main()
{
    this.setSize(dim);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setLayout(null);

    ImageIcon img = new ImageIcon("C:\\Users\\Cy\\Pictures\\1.png");
    JLabel imgLabel1 = new JLabel(img);
    this.getLayeredPane().add(imgLabel1, new Integer(Integer.MIN_VALUE));
    imgLabel1.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
    Container contain = this.getContentPane();
    ((JPanel) contain).setOpaque(false);

    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.setBackground(c);
    but_cal.setOpaque(false);
    but_ret.setOpaque(false);
    but_cal.setBorderPainted(false);
    but_ret.setBorderPainted(false);
    but_cal.setBounds(width-height/10, height*2/7-height/10, 100,100);
    ImageIcon icon2 =new ImageIcon("C:\\\\Users\\\\Cy\\\\Pictures\\\\cal.png");         //原来的背景
    but_cal.setIcon(icon2);
    but_cal.addActionListener(this);

}

public void actionPerformed(ActionEvent e)
{
    ImageIcon img = new ImageIcon();
    if(e.getSource() == but_cal)
    {
        JLabel imgLabel2=new JLabel();
        img = new ImageIcon("C:\\Users\\Cy\\Pictures\\star.png");  //新的背景
        imgLabel2.setIcon(img);
        imgLabel2.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
        this.add(imgLabel2);
        but_cal.setVisible(false);
        but_ret.setBounds(0,0, width/20, height/20);
        ImageIcon icon0 =new ImageIcon("C:\\\\Users\\\\Cy\\\\Pictures\\\\ret.png");
        but_ret.setIcon(icon0);
        this.setVisible(false);
        this.setVisible(true);
        but_ret.addActionListener(this);
    }
    if(e.getSource() == but_ret)
    {
        but_ret.setVisible(false);
        but_cal.setVisible(true);
        JLabel imgLabel1=new JLabel();
        img = new ImageIcon("C:\\Users\\Cy\\Pictures\\1.png");    //原来的背景
        imgLabel1.setIcon(img);
        imgLabel1.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
        this.add(imgLabel1);
        this.invalidate();
        this.validate();
        this.setVisible(false);
        this.setVisible(true);

    }

}

}


import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

class JF extends JFrame{
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 b1=Box.createHorizontalBox();

    b1.add(Box.createVerticalStrut(200));
    b1.add(jButton);
    b1.add(Box.createHorizontalStrut(40));
    b1.add(jButton2);

    jf.add(b1);    //将外层横向Box添加进窗体
    jf.setVisible(true);

    jf.setBounds(100,100,400,200);
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    jButton.addActionListener(new ActionListener() {
      @Override
      public 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.getIconHeight()); // todo: 把这里改为换图片就行了
        //jf.getContentPane().setBackground(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);
            imgLabel2.setBounds(0, 0, img2.getIconWidth(), img2.getIconHeight());
            jf.add(imgLabel2);
            //jf.getContentPane().setBackground(Color.BLACK);  //todo: 把这里改为换图片就行了
      }
    });

  }
}
  • 写回答

1条回答 默认 最新

  • ck_nikita 2020-07-14 20:56
    关注

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

    class JF extends JFrame{
      public JF(){
      }
    }
    
    public class MyTest3  {
      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 b1=Box.createHorizontalBox();
    
        b1.add(Box.createVerticalStrut(200));
        b1.add(jButton);
        b1.add(Box.createHorizontalStrut(40));
        b1.add(jButton2);
    
        jf.add(b1);    //将外层横向Box添加进窗体
        jf.setVisible(true);
        jf.setBounds(100,100,400,200);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        jButton.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            jf.getContentPane().setBackground(Color.RED); // todo: 把这里改为换图片就行了
          }
        });
    
        jButton2.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            jf.getContentPane().setBackground(Color.BLACK);  //todo: 把这里改为换图片就行了
          }
        });
    
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料