日期:2014-05-20  浏览次数:20647 次

java awt如何添加背景图片
RT,用awt添加一个Frame,再在它上面加一张背景图片。
注:不能用swing


------解决方案--------------------
Java code

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;



public class BGTest extends Frame{

    private BufferedImage bgImage;
    public BGTest() throws Exception{
        this.setTitle("bg");
        this.setSize(400, 600);
        this.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        bgImage = ImageIO.read(new File("c:/psb.jpg"));
    }
    
    @Override
    public void paint(Graphics g) {
        g.drawImage(bgImage, 0, 0, this.getWidth(), this.getHeight(), this);
    }
    
    public static void main(String[] args) {
        try {
            new BGTest().setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

------解决方案--------------------
+1
探讨
Java code

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;

……