2008年(3500)
分类:
2008-05-04 19:15:09
1.创建一个类ImagePanel,代码如下
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.Graphics2D;
class ImagePanel extends JPanel {
public ImagePanel() {
try {
File f = new File("Img/Login.gif");
img = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
}
private Image img;
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if (img != null) {
g2.drawImage(img, 0, 0, getWidth(), getHeight(),
0, 0, img.getWidth(null), img.getHeight(null), null);
}
}
}
在新建的JPanel面板中调用:
把public class Welcome
extends JPanel
改为public class Welcome
extendsImagePanel
即可实现背景.
在对话框中使用如下:
public class Login
extends JDialog {
String pass;
static String name;
ImagePanel panel1 = new ImagePanel(); //使用ImagePanel类载入的背景替换默认Jpanel1做为背景