这是个用Swing写的QQ界面,可是图片一直显示不了,图片放在项目文件下img文件夹下,求高手帮忙解决啊!
/*
* TestQQ类将用于建立QQ登录界面
*
* 2008.5.16
*/package mypro;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class QQTest extends JFrame {
Container contentPane;
ImageIcon img = new ImageIcon("img/1111.gif");
JPanel paneTop = new JPanel();
JPanel paneMid = new JPanel();
JPanel paneBut = new JPanel();
JPanel paneAll = new JPanel();
JLabel lblTop = new JLabel();
JLabel lblName = new JLabel();
JLabel lblPwd = new JLabel();
JLabel lblApply = new JLabel();
JLabel lblForget = new JLabel();
JLabel lblModel = new JLabel();
JLabel lblNull = new JLabel();
JLabel lblNull1 = new JLabel();
JTextField txtName = new JTextField(15);
JPasswordField txtPwd = new JPasswordField(15);
JComboBox cmb = new JComboBox();
JCheckBox chk = new JCheckBox();
JButton btnKill = new JButton("查杀木马");
JButton btnSet = new JButton("设置");
JButton btnLogin = new JButton("登录");
public QQTest(){
lblTop.setIcon(img);//顶部添加图片
paneTop.add(lblTop);
lblName.setText("QQ帐号:");//中部添加非按钮控件
lblApply.setText("申请帐号 ");
lblPwd.setText("QQ密码:");
lblForget.setText("忘记密码?");
lblModel.setText("状态:");
String[] s1 = {"隐身","在线","忙碌"};
cmb.addItem(s1[0]);
cmb.addItem(s1[1]);
cmb.addItem(s1[2]);
chk.setText("自动登录");
paneMid.add(lblName);
paneMid.add(txtName);
paneMid.add(lblApply);
paneMid.add(lblPwd);
paneMid.add(txtPwd);
paneMid.add(lblForget);
paneMid.add(lblModel);
paneMid.add(cmb);
paneMid.add(chk);
paneBut.add(btnKill);//底部添加按钮控件
paneBut.add(btnSet);
paneBut.add(lblNull1);
paneBut.add(btnLogin);
contentPane = this.getContentPane();
contentPane.add(paneTop,BorderLayout.NORTH);//使用BorderLayout布局顶部添加在North
contentPane.add(paneMid,BorderLayout.CENTER);//中部添加非按钮控件
contentPane.add(paneBut,BorderLayout.SOUTH);//底部添加按钮控件
setTitle("欢迎使用QQ");
setSize(300,240);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getSize().width)/2,(screen.height - getSize().height)/2 );
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
QQTest d = new QQTest();
}
}
/*
* TestQQ类将用于建立QQ登录界面
*
* 2008.5.16
*/package mypro;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class QQTest extends JFrame {
Container contentPane;
ImageIcon img = new ImageIcon("img/1111.gif");
JPanel paneTop = new JPanel();
JPanel paneMid = new JPanel();
JPanel paneBut = new JPanel();
JPanel paneAll = new JPanel();
JLabel lblTop = new JLabel();
JLabel lblName = new JLabel();
JLabel lblPwd = new JLabel();
JLabel lblApply = new JLabel();
JLabel lblForget = new JLabel();
JLabel lblModel = new JLabel();
JLabel lblNull = new JLabel();
JLabel lblNull1 = new JLabel();
JTextField txtName = new JTextField(15);
JPasswordField txtPwd = new JPasswordField(15);
JComboBox cmb = new JComboBox();
JCheckBox chk = new JCheckBox();
JButton btnKill = new JButton("查杀木马");
JButton btnSet = new JButton("设置");
JButton btnLogin = new JButton("登录");
public QQTest(){
lblTop.setIcon(img);//顶部添加图片
paneTop.add(lblTop);
lblName.setText("QQ帐号:");//中部添加非按钮控件
lblApply.setText("申请帐号 ");
lblPwd.setText("QQ密码:");
lblForget.setText("忘记密码?");
lblModel.setText("状态:");
String[] s1 = {"隐身","在线","忙碌"};
cmb.addItem(s1[0]);
cmb.addItem(s1[1]);
cmb.addItem(s1[2]);
chk.setText("自动登录");
paneMid.add(lblName);
paneMid.add(txtName);
paneMid.add(lblApply);
paneMid.add(lblPwd);
paneMid.add(txtPwd);
paneMid.add(lblForget);
paneMid.add(lblModel);
paneMid.add(cmb);
paneMid.add(chk);
paneBut.add(btnKill);//底部添加按钮控件
paneBut.add(btnSet);
paneBut.add(lblNull1);
paneBut.add(btnLogin);
contentPane = this.getContentPane();
contentPane.add(paneTop,BorderLayout.NORTH);//使用BorderLayout布局顶部添加在North
contentPane.add(paneMid,BorderLayout.CENTER);//中部添加非按钮控件
contentPane.add(paneBut,BorderLayout.SOUTH);//底部添加按钮控件
setTitle("欢迎使用QQ");
setSize(300,240);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width - getSize().width)/2,(screen.height - getSize().height)/2 );
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]){
QQTest d = new QQTest();
}
}
可能是你的目录写错了 "./img/1111.gif" 试试 .表示当前目录 要不然你要把目录写全
2楼 它的 路径格式那样写是没有问题的 你的也对 两种都行 楼主 只要把 ImageIcon img = new ImageIcon("img/1111.gif"); 替换成 ImageIcon img = new ImageIcon(".img/1111.gif");
相信就OK了
img.setImageObserver(lblTop);看看
│ QQTest.class
│
└─img
1111.gif
把你的img文件夹放在你的工程里面啊 应该就可以了 还有看下你的图片的格式是不是。gif
把你的img文件夹放在你的工程里面啊 应该就可以了 还有看下你的图片的格式是不是。gif
我已经测试过了 其他地方是没有问题的
ImageIcon img = new ImageIcon("img/1111.gif");
改成 ImageIcon img = new ImageIcon(".img/1111.gif"); 多一个点 他表示 当前项目所在目录 除此外你还要确认几个地方 1、你的img文件夹是在项目的根目录吗
2、图片名 尤其是后缀 你要是模仿 QQ登录窗口 那图片应该是静态 怎么要用 gif 我在Eclipse中测试过你的程序 ImageIcon img = new ImageIcon("./image/login.jpg"); // image 是我项目下的子文件夹 login.jpg 是一张图片 结果一切正常。