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: 把这里改为换图片就行了
}
});
}
}