为Java程序添加漂亮背景图片

2014-11-23 19:27:26 · 作者: · 浏览: 28

  整理后可执行代码如下:


  import java.awt.*;


  import javax.swing.*;


  public class TestBackgroundColor extends JFrame


  {


  public static void main(String[] args)


  {


  // TODO Auto-generated method stub


  TestBackgroundColor tbc = new TestBackgroundColor();


  tbc.setVisible(true);


  }


  private JPanel imagePanel;


  private ImageIcon background;


  public TestBackgroundColor()


  {


  background = new ImageIcon("渐变背景14.png");//背景图片


  JLabel label = new JLabel(background);//把背景图片显示在一个标签里面


  //把标签的大小位置设置为图片刚好填充整个面板


  label.setBounds(0,0,background.getIconWidth(),background.getIconHeight());