日期:2014-05-20 浏览次数:21012 次
package com.wwe.w96;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class AutoImageSizeByForm extends JFrame {
public AutoImageSizeByForm() {
setTitle("AutoImageSizeByForm");
setBounds(100, 100, 450, 300);
Image img = new ImageIcon("psb.jpeg").getImage();
ShadePanel contentPanel = new ShadePanel(img);
this.add(contentPanel);
setVisible(true);
}
public static void main(String[] args) {
AutoImageSizeByForm application = new AutoImageSizeByForm();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class ShadePanel extends JPanel {
Image image;
public ShadePanel(Image image) {
this.image = image;
}
public void paint(Graphics g) {
super.paint(g);
if (image != null) {
int width = getWidth();
int height = getHeight();
g.drawImage(image, 0, 0, width, height, this);
}
}
}