JLabel设置背景颜色

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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值