关于ImageIcon显示图片的问题

帅文浩 2013-06-20 03:54:12
编了个简单的界面,可是图片显示不出来,路径和文件没绝对没有错误。麻烦大家看看哪出了问题!
public class Welcome extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;

public Welcome(){
JFrame welcome = new JFrame("系统");
welcome.setResizable(false);
Container container = welcome.getContentPane();
container.setLayout(null);

/*
* 管理员用户登录
*/
JMenuBar mbadmin = new JMenuBar();
mbadmin.setLayout(new FlowLayout(FlowLayout.LEFT));
mbadmin.setBounds(0, 0, 700, 30);

JMenu mnadmin = new JMenu("管理员入口");

JMenuItem mis = new JMenuItem("甲");
JMenuItem mih = new JMenuItem("乙");
JMenuItem miq = new JMenuItem("丙");
JMenuItem mix = new JMenuItem("丁");

mnadmin.add(mis);
mnadmin.add(mih);
mnadmin.add(miq);
mnadmin.add(mix);

mbadmin.add(mnadmin);
mbadmin.setOpaque(true);
/*
* 欢迎界面
*/
Icon cnimage = new ImageIcon("D:\\Documents\\Downloads\\getimage.jpg");
JLabel lbwelcome = new JLabel(cnimage,JLabel.CENTER);
JPanel pnwelcome = new JPanel();
pnwelcome.add(lbwelcome);
pnwelcome.setBounds(0, 100, 700, 300);

JButton btgoon = new JButton("继续");
JPanel pngoon = new JPanel();
pngoon.setLayout(new FlowLayout());
pngoon.add(btgoon);
pngoon.setBounds(0, 400, 700, 50);

container.add(mbadmin);
container.add(lbwelcome);
container.add(pngoon);

welcome.setBounds(250, 100, 700, 500);
welcome.show();
welcome.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
...全文
718 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
帅文浩 2013-07-04
  • 打赏
  • 举报
回复
引用 4 楼 my_panpan 的回复:
引用
container.add(lbwelcome);
图片你加到了panel上,但是这里往面板上加的是图标Label不是图标panel,改成container.add(pnwelcome); 直接往面板上加图标的Label也可以,但是因为你的布局是null,所以你要给图标lable设置位置大小,所以说要用container.add(lbwelcome)也可以,但是要lbwelcome.setBounds(0, 100, 700, 300);
辛苦了,其实没那么麻烦。谢谢!
帅文浩 2013-07-04
  • 打赏
  • 举报
回复
引用 7 楼 y541028105 的回复:
[quote=引用 3 楼 m243723424 的回复:] 呵图片就没有往控件上加
他new JLable的时候传的参数就是图片[/quote]看来等级说明不了问题,分给知音了
帅文浩 2013-07-04
  • 打赏
  • 举报
回复
引用 6 楼 LCL_data 的回复:
[quote=引用 2 楼 Bruce_Shine 的回复:] 我已经找到问题所在了,谢谢 50行,改成add(pnwelcome)
解决了,就早点结贴吧。 结贴是个好习惯[/quote]要多少分?
苦瓜_Young 2013-06-28
  • 打赏
  • 举报
回复
引用 3 楼 m243723424 的回复:
呵图片就没有往控件上加
他new JLable的时候传的参数就是图片
十八道胡同 2013-06-27
  • 打赏
  • 举报
回复
引用 2 楼 Bruce_Shine 的回复:
我已经找到问题所在了,谢谢 50行,改成add(pnwelcome)
解决了,就早点结贴吧。 结贴是个好习惯
huntor 2013-06-26
  • 打赏
  • 举报
回复
继承了JFrame,为什么还要new JFrame???
my_panpan 2013-06-26
  • 打赏
  • 举报
回复
引用
container.add(lbwelcome);
图片你加到了panel上,但是这里往面板上加的是图标Label不是图标panel,改成container.add(pnwelcome); 直接往面板上加图标的Label也可以,但是因为你的布局是null,所以你要给图标lable设置位置大小,所以说要用container.add(lbwelcome)也可以,但是要lbwelcome.setBounds(0, 100, 700, 300);
Bettygonna 2013-06-20
  • 打赏
  • 举报
回复
呵图片就没有往控件上加
帅文浩 2013-06-20
  • 打赏
  • 举报
回复
我已经找到问题所在了,谢谢 50行,改成add(pnwelcome)
suixinsuoyugirl 2013-06-20
  • 打赏
  • 举报
回复
路径有问题吧,我用过相对路径分隔符是“/”,你可以试试~~
图片浏览器 (后面附有完整代码) 一、需求分析 1、首先。因为要显示图片所以要先有一个界面;用JFrame; 2、因为要能显示选定文件夹内的图片,所以要有一个文件夹选择器;用JTree; 3、显示图片的滚动面板;用JScrollPane; 4、因为要对图片进行分类,所以要设计分类模块; 5、因为要对图片进行备份,所以要设计备份模块; 6、因为要对图片进行幻灯播放,所以要设计幻灯播放模块; 二、概要设计 本次课程设计模块图、系统流程图: 三、运行环境、开发语言 操作系统:Windows XP 专业版 32位 SP3 ( DirectX 9.0c ) 开发工具:MyEclipse 开发语言:java 四、详细设计 1 程序清单 String pt;鼠标点击的图片的绝对路径 File[] files2;面板上正在显示图片文件 JLabel[] imageLabel;图片绑定的JLabel控件组 JLabel jlabel,jimageLabel; 图片绑定的JLabel控件 JPanel jp1,jp2,jp3;显示不同内容的面板 JButton[] jb;按钮数组 private JTree tree;文件夹选择树 private JScrollPane jsp1,jsp2,jsp3;滚动面板 Timer timer;计时器 public PictureDir()类的构造函数 WindowAction窗体事件 TreeSelect文件夹选择树事件 FileDidianListener分类为地点事件 FileRenwuListener分类为人物事件 FileXiangceListener分类为相册事件 FileQitaListener分类为其它事件 FileZipListener备份事件 FileBackListener还原事件 MouseAction鼠标点击事件 FileStartListener幻灯片播放事件 JCAction显示分类图片事件 read(String)读文件函数 save(String,String)写文件函数 unzip(String,String)解压缩函数 resizeIcon(ImageIcon,JLabel)控制图片显示大小的函数 deleteFile(File)删除文件函数 main(String[])主函数 2 主要代码 整个程序的界面如下图: 2.1 选择文件夹功能 class TreeSelect implements TreeSelectionListener{ public void valueChanged(TreeSelectionEvent e) { TreePath path=e.getPath(); DefaultMutableTreeNode node=(DefaultMutableTreeNode)path.getLastPathComponent(); Object userObject=node.getUserObject(); if(!(userObject instanceof File)){ return; } File folder=(File)userObject; if(!folder.isDirectory()) return; File[] files1=initImageSets1(folder); for(File file:files1){ node.add(new DefaultMutableTreeNode(file)); } files2=initImageSets2(folder); for(int i=0;iImageIcon(files2[i].toString())); imageLabel[i].addMouseListener(new MouseAction()); imageLabel[i].setName(String.valueOf(i)); resizeIcon(new ImageIcon(files2[i].toString()),imageLabel[i]); } } private File[] initImageSets1(File pictureDir){ return pictureDir.listFiles(new FilenameFilter(){ public boolean accept(File dir,Strin
Java在窗口上加载显示GIF动画图像,将多个独立的GIF图像串联在一起显示,形成GIF特有的动画形式。主要代码如下:   ImageIcon[] images; //用于动画的图标数组   Timer animationTimer;   int currentImage = 0; //当前图像编号   int delay = 500; //图像切换延迟   int width; //图像宽度   int height; //图像高度   public AnimatorIcon() //构造函数   {    setBackground(Color.white);    images = new ImageIcon[2]; //初始化数组    for (int i=0;i   images[i]=new ImageIcon(getClass().getResource("image" i ".gif")); //实例化图标    width = images[0].getIconWidth(); //初始化宽度值    height = images[0].getIconHeight(); //初始化高度值   }   public void paintComponent(Graphics g) { //重载组件绘制方法    super.paintComponent(g); //调用父类函数    images[currentImage].paintIcon(this,g,70,0); //绘制图标    currentImage=(currentImage 1)%2; //更改当前图像编号   }   public void actionPerformed(ActionEvent actionEvent) {    repaint();   }   public void startAnimation() { //开始动画    if (animationTimer==null) {    currentImage=0;    animationTimer=new Timer(delay, this); //实例化Timer对象    animationTimer.start(); //开始运行    } else if (!animationTimer.isRunning()) //如果没有运行    animationTimer.restart(); //重新运行   }   public void stopAnimation() {    animationTimer.stop(); //停止动画   }   public static void main(String args[]) {    AnimatorIcon animation = new AnimatorIcon(); //实例化动画图标    JFrame frame = new JFrame("动画图标"); //实例化窗口对象    frame.getContentPane().add(animation); //增加组件到窗口上    frame.setSize(200, 100); //设置窗口尺寸    frame.setVisible(true); //设置窗口可视    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序    animation.startAnimation(); //开始动画
package cn.com.edu.view.frame; import java.awt.AWTException; import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Dimension; import java.awt.GridBagLayout; import java.awt.MenuItem; import java.awt.PopupMenu; import java.awt.SystemTray; import java.awt.Toolkit; import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JToolBar; import org.jvnet.substance.SubstanceLookAndFeel; import org.jvnet.substance.skin.FindingNemoSkin; import cn.com.edu.action.JMenuItemAction; import cn.com.edu.action.MainFrameAction; import cn.com.edu.util.GBC; import cn.com.edu.view.panel.AddStudentInfoPanel; import cn.com.edu.view.panel.FindStudentInfoPanel; /** * 教务管理系统主界面 * * @author Administrator * */ public class MainFrame extends JFrame { private static MainFrame instance; private JMenuBar bar;// 菜单条 private JMenu studentJMenu;// 菜单 private JMenu teacherJMenu;// 菜单 private JPanel center = new JPanel();// 中心面板用来放置卡片 private CardLayout card = new CardLayout();// 卡片布局 private JPanel west;// 西边面板 private JSplitPane split;// 分割面板 private JToolBar tool;// 工具条 private MainFrameAction action = new MainFrameAction(this);// 按钮事件对象 private JMenuItemAction menuItemAction = new JMenuItemAction(this);// 菜单事件对象 private SystemTray tray;// 系统托盘 private TrayIcon trayIcon;// 设置系统托盘的图片 /** * 使用单子设计模式主界面对象 * */ private MainFrame() { init(); } public static MainFrame getInstance() { if (instance == null) { instance = new MainFrame(); } return instance; } /** * 初始化主界面 * */ public void init() { // 设置标题 this.setTitle("教务管理系统"); // 设置标题图片 ImageIcon icon = new ImageIcon("img/switchuser.png"); this.setIconImage(icon.getImage()); // 得到屏幕对象 Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); // 设置主界面大小 this.setSize(size.width, size.height - 20); // 设置居中 this.setLocationRelativeTo(null); // 添加工具条 this.add(createTool(), BorderLayout.NORTH); // 将菜单添加到主界面 this.setJMenuBar(createJMenuBar()); // 将卡片添加到主界面 center.setLayout(card); addCardPanel(center); this.add(createSplit()); // 设置关闭主界面 this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE); //创建系统托盘 createSystemTray(); //关闭窗口事件 closeWindow(this); // 设置显示主界面 this.setVisible(true); } public JMenuBar createJMenuBar() { if (bar == null) { bar = new JMenuBar(); studentJMenu = createJMenu("学生管理"); teacherJMenu = createJMenu("老师管理"); addJMenuItem(studentJMenu, "添加学生信息"); addJMenuItem(studentJMenu, "查询学生信息"); addJMenuItem(studentJMenu, "修改学生信息"); addJMenuItem(studentJMenu, "删除学生信息"); studentJMenu.addSeparator(); addJMenuItem(studentJMenu, "退出"); bar.add(studentJMenu); bar.add(teacherJMenu); } return bar; } /** * 创建菜单 * * @param name * @return */ private JMenu createJMenu(String name) { JMenu menu = new JMenu(name); return menu; } /** * 将创建的菜单项添加到菜单 * * @param menu * @param name */ private void addJMenuItem(JMenu menu, String name) { JMenuItem item = new JMenuItem(name); item.addActionListener(menuItemAction); menu.add(item); } /** * 用于添加卡片 * * @param center */ public void addCardPanel(JPanel center) { JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); jp2.add(new JButton("卡片2")); jp3.add(new JButton("卡片3")); jp4.add(new JButton("卡片4")); center.add(new AddStudentInfoPanel(), "添加学生信息"); center.add(new FindStudentInfoPanel(), "查询学生信息"); center.add(jp3, "修改学生信息"); center.add(jp4, "删除学生信息"); } /** * 创建西边面板,用添加选项按钮 * * @return */ public JPanel createWestPanel() { if (west == null) { west = new JPanel(); west.setLayout(new GridBagLayout()); west.add(createButton("添加学生信息", "img/switchuser.png"), new GBC(0, 0).setInset(10)); west.add(createButton("查询学生信息", "img/switchuser.png"), new GBC(0, 1).setInset(10)); west.add(createButton("修改学生信息", "img/switchuser.png"), new GBC(0, 2).setInset(10)); west.add(createButton("删除学生信息", "img/switchuser.png"), new GBC(0, 3).setInset(10)); } return west; } /** * 创建按钮方法 * * @param name * @return */ public JButton createButton(String name, String icon) { JButton button = new JButton(name); button.setIcon(new ImageIcon(icon)); button.addActionListener(action); return button; } public CardLayout getCard() { return card; } public JPanel getCenter() { return center; } /** * 分割面板 * * @return */ public JSplitPane createSplit() { if (split == null) { split = new JSplitPane(); split.setOneTouchExpandable(true); split.setLeftComponent(createWestPanel()); split.setRightComponent(center); } return split; } /** * 创建工具条 * * @return */ public JToolBar createTool() { if (tool == null) { tool = new JToolBar(); tool.add("添加学生信息", createButton("添加学生信息", "img/switchuser.png")); tool.add("查询学生信息", createButton("查询学生信息", "img/switchuser.png")); tool.add("修改学生信息", createButton("修改学生信息", "img/switchuser.png")); tool.add("删除学生信息", createButton("删除学生信息", "img/switchuser.png")); tool.add("帮助", createButton("帮助", "img/syssetup.png")); } return tool; } ///////////////////////////系统托盘设置///////////////////////////////////// /** * 窗口事件 * * @param jframe */ public void closeWindow(MainFrame jframe) { jframe.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { int show = JOptionPane.showConfirmDialog(null, "是否确定关闭?", "确认关闭系统", JOptionPane.YES_NO_OPTION); if (show == JOptionPane.YES_OPTION) { System.exit(0); } } public void windowIconified(WindowEvent e) { if (getState() == 1) {// 最小化 try { tray.add(trayIcon); } catch (AWTException e1) { e1.printStackTrace(); } setVisible(false); } } }); } /** * 创建系统托盘 * */ public void createSystemTray() { // 得到当前系统的托盘对象 tray = SystemTray.getSystemTray(); ImageIcon icon = new ImageIcon("img/2.png"); // 添加鼠标右键 弹出菜单 PopupMenu menu = new PopupMenu(); MenuItem show = new MenuItem("显示窗体"); MenuItem exit = new MenuItem("退出窗体"); trayIcon = new TrayIcon(icon.getImage(), "学生管理系统", menu); trayIcon.addMouseListener(new MouseAdapter() { /** * 鼠标点击事件 */ public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) {// 鼠标双击 tray.remove(trayIcon); setVisible(true); // 设置窗口全屏 setExtendedState(JFrame.MAXIMIZED_BOTH); } } }); /** *鼠标右键显示窗体 */ show.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { tray.remove(trayIcon); setVisible(true); // 设置窗口全屏 setExtendedState(JFrame.MAXIMIZED_BOTH); } }); /** * 鼠标右键关闭窗体 */ exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int show = JOptionPane.showConfirmDialog(null, "是否确定关闭?", "确认关闭系统", JOptionPane.YES_NO_OPTION); if (show == JOptionPane.YES_OPTION) { System.exit(0); } } }); menu.add(show); menu.add(exit); } /** * @param args */ public static void main(String[] args) { SubstanceLookAndFeel.setSkin(new FindingNemoSkin()); // 蓝色幽灵 // SubstanceLookAndFeel.setSkin(new OfficeBlue2007Skin()); // 麦田风光 // SubstanceLookAndFeel.setSkin(new FieldOfWheatSkin()); // 默认皮肤 // SubstanceLookAndFeel.setSkin(new BusinessSkin()); // 朦胧风格 // SubstanceLookAndFeel.setSkin(new MistAquaSkin()); MainFrame.getInstance(); } }

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧