new ImageIcon(String filename)的路径问题

比如我新建一个工程CoreJava。其中有CoreJava/bin,CoreJava/src两个文件夹。

如果图像文件放在CoreJava文件夹内,则书写格式为:

new ImageIcon("yellow-ball.gif");

如果放在CoreJava/src文件夹内,则书写格式为:

new ImageIcon("src/yellow-ball.gif");

=============================

 

URL myUrl=getClass().getResource("../images/iconcs_003.png");
ImageIcon myIcon=new ImageIcon(myUrl);
jLabel1.setIcon(myIcon); 注意这里../的使用,在路径的引用方面,java完全采用了传统的方式,即./表示当前路径,而../表示上级路径,so,images包是和当前类所在包平级的一个包,所以在这里采用如此的写法,才能够引用到所需要的图片,如果图片和当前类在同一个包中,那么可以写成直接些图片的名称即可。

 

 

给下面的代码写注释try { stmt = con.createStatement(); String sql = "select * from building where number='"+number+"'; "; rs = stmt.executeQuery(sql); } catch (SQLException e2) { e2.printStackTrace(); } String imgPath=""; try { if(rs.next()) { String num = rs.getString("number"); String location = rs.getString("location"); String name = rs.getString("name"); String space = rs.getString("space"); String color = rs.getString("color"); String img= rs.getString("img"); imgPath = rs.getString("img"); String username= rs.getString("username"); field1.setText(num); field1.setEnabled(false); field2.setText(location); //field2.setEnabled(false); field3.setText(username); //field3.setEnabled(false); field4.setText(space); //field4.setEnabled(false); field5.setText(color); //field5.setEnabled(false); } else { JOptionPane.showMessageDialog(null,"没有此编号的民居的详细信息!"); new Guanliyuan(true); } } catch (HeadlessException e2) { e2.printStackTrace(); } catch (SQLException e2) { e2.printStackTrace(); } if(imgPath!=""&&imgPath!=null) { File file3 = new File(imgPath); try { buffimg = ImageIO.read(file3); } catch (IOException a) { a.printStackTrace(); } filename = file3.getName(); String cat = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); if (cat.equalsIgnoreCase("JPG") || cat.equalsIgnoreCase("GIF") || cat.equalsIgnoreCase("PNG") || cat.equalsIgnoreCase("JPEG")) { ic = new ImageIcon(imgPath); ia = ic.getImage(); int height = ic.getIconHeight(); int width = ic.getIconWidth(); jLabelImg.setSize(width, height); jLabelImg.setIcon(ic); repaint(); } else { JOptionPane.showMessageDialog(null, " 该软件只支持 JPG 、GIF 、 PNG格式的图像文件 ", " 提示 ", JOptionPane.INFORMATION_MESSAGE); } } }
最新发布
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值