弄了好几天都得不到java项目的背景图片
package com.lwa;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Entry extends JFrame {
private JTextField namejTextField;
private JTextField passwordjTextField;
public static void main(String args[]){
EventQueue.invokeLater(new Runnable(){
public void run(){
try{
Entry frame=new Entry();
frame.setTitle("广西民族师范学院图书馆登陆窗口");
frame.setVisible(true);
}catch(Exception e){
e.printStackTrace();
}
}
});
}
public Entry(){
super();
getContentPane().setLayout(null);
setBounds(0,0,352,190);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
container();
//添加背景图片
ImageIcon img=new ImageIcon(getClass().getResource("photo.jpg"));
final JLabel backLabel=new JLabel();
backLabel.setBounds(0,0,img.getIconWidth(),img.getIconHeight());
setBounds(100,100,img.getIconWidth(),img.getIconHeight()+30);
backLabel.setIcon(img);
getContentPane().add(backLabel);
//添加用户名文本标签
namejTextField=new JTextField();
namejTextField.setBounds(115,12,172,22);
getContentPane().add(namejTextField);
//添加密码文本标签
passwordjTextField=new JTextField();
passwordjTextField.setBounds(115,40,172,22);
getContentPane().add(passwordjTextField);
//添加“用户名”
final JLabel label=new JLabel();
label.setText("用户名:");
label.setBounds(60,12,60,20);
getContentPane().add(label);
//添加“密码”
final JLabel label2=new JLabel();
label2.setText("密码:");
label2.setBounds(60,40,50,15);
getContentPane().add(label2);
JPasswordField passwordField=new JPasswordField("mrsoft");
//passwordField.setEchoChar("#");
passwordField.setColumns(10);
getContentPane().add(passwordField);
}
public Container getcontainer(){
Container cc=new Container();
//设置“登陆”按钮
final JButton jButton=new JButton("登陆");
jButton.setBounds(60,84,69,28);
cc.add(jButton);
//设置“重置”按钮
final JButton jButton2=new JButton("重置");
jButton2.setBounds(200,84,69,28);
cc.add(jButton2);
return cc;
}
public void container(){
this.setContentPane(getcontainer());
}
}
我已经把图片放在bin目录下了,还是不得。
小女实在不才!请各位高手帮忙
[解决办法]
ImageIcon img=new ImageIcon(getClass().getResource("photo.jpg"));
这是相对路径,和你的类 Entry 在同一目录下