package net.image.test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
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;
public class Whatever extends JFrame
{
public ImageIcon firstImg;
JLabel image;
public int i = 0;
public Whatever() {
super("whatever");
Container container = getContentPane();
//container.setLayout( new FlowLayout() );
//container.setLayout(null);
JButton btn = new JButton("change");
firstImg = new ImageIcon("d:/pic0.jpg");
image = new JLabel(firstImg);
image.setBackground(new Color(255,255,255));
image.setOpaque(true); //这里是必须的
container.add(image,BorderLayout.CENTER);
//Image1.setBounds(10,10,150,150);
container.add(btn,BorderLayout.SOUTH);
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
image.removeAll();
i = (i+1)%4;
firstImg = new ImageIcon("d:/pic"+i+".jpg");
//firstImg = new ImageIcon();//移除背景图
image.setIcon(firstImg);
}
});
setSize(800,600);
setVisible(true);
}
public static void main( String args[] )
{
Whatever application = new Whatever();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
JLabel背景图片替换
//移除显示的图片,并显示为空白,供js调用
public void removePicture(){
label.removeAll();
ImageIcon icon = new ImageIcon(); //替换为空白
ImageIcon icon = new ImageIcon(path);//替换为其他图片,path要与上次不同(图片名不同)
label.setIcon(icon);
}