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

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

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条回答 默认 最新

  • ambush0
    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: 把这里改为换图片就行了
          }
        });
    
      }
    }
    
    点赞 评论

相关推荐