swing图形化界面,使用JLable和JPanel设置背景图片时,图片无法显示
package com.systom;
import javax.swing.*;
import java.awt.*;
/**
* @author Nidhogg
*/
public class LoginStart extends JFrame{
/**背景图片标签*/
JLabel backgroundImg;
/**标题标签*/
JLabel title;
/**账号标签*/
JLabel jLabel1;
/**密码标签*/
JLabel jLabel2;
/**注册标签*/
JLabel jLabel3;
/**账号文本*/
JTextField jTextField;
/**密码文本*/
JPasswordField jPasswordField;
final int WIDTH = 750;
final int HEIGHT = 422;
JPanel jPanel1 ;
public LoginStart() {
this.init();
this.setName("竞赛项目管理系统");
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.validate();
}
public void init() {
jPanel1 = new JPanel();
jPanel1.setBounds(0,0,WIDTH,HEIGHT);
jPanel1.setLayout(null);
/*设置界面出现的位置*/
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
int width = dimension.width;
int height = dimension.height;
int x = (width - WIDTH)/2;
int y = (height - HEIGHT)/2;
this.setBounds(x,y,WIDTH,HEIGHT);
/*设置背景图片*/
ImageIcon imageIcon = new ImageIcon("C:\\Users\\Nidhogg\\IdeaProjects\\竞赛项目管理系统\\src\\img\\a.jpg");
backgroundImg = new JLabel(imageIcon);
backgroundImg.setBounds(0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight());
jPanel1.add(backgroundImg);
this.add(jPanel1);
}
}
将图片显示出来就可以了