可以新建个面板,在面板里放入带图片的JLabel,填满面板即可。
JPanel jp = new JPanel(); //新建面板
jp.setLayout(new FlowLayout()); //设置面板布局
ImageIcon ii=new ImageIcon(getClass().getResource("/Picture/i.jpg"));
JLabel uppicture=new JLabel(ii); //往面板里加入JLabel
this.setVisible(true);
import java.awt.EventQueue;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.ImageIcon;
public class Login extends JFrame {
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setVisible(true);//显示窗体即窗体可见
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Login() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
ImageIcon backIcon=new ImageIcon(getClass().getResource("background.jpg"));
final JLabel backLabel=new JLabel(backIcon); // 创建背景标签
backLabel.setSize(backIcon.getIconWidth(), backIcon.getIconHeight());
backLabel.setLocation(0, 0);
// 设置标签定位
getContentPane().add(backLabel); // 添加标签到窗体
setResizable(true);//可以改变窗体大小
}
}
class
p
extends
jpanel
{
public
void
paint(graphics
g)
{
super.paint(g);
try
{
file
f
=
new
file("g:\\4.jpg");
url
url
=
f.touri().tourl();
imageicon
img
=
new
imageicon(url);
g.drawimage(img.getimage(),
0,
0,
this);
}
catch
(exception
e)
{
}
}
}
继承jpanel
重写paint方法.
如果是label的话,有属性可以添加背景图片
楼上讲的很详细