猿问

您好,求问该怎样在java里用URL引入图片引入图片 ?

如题,网上查了很久都找不到答案。
我是要建一个JFrame窗口,然后在里面的JLabel处引入图片,已经在src下捡了个子目录images来放图片的,但是不知道怎样引入,求高手教一教。如果可以的话顺便教教我怎样用相对路径引用

慕尼黑8549860
浏览 360回答 3
3回答

胡子哥哥

URL对象中前而几个方法都非常容易理解,而该对象提供的openStream()可以读取该 URL资源的InputStream,通过该方法可以非常方便地读取远程资源。下面的程序示范如何通过URL类读取远程资源:1)只显示网络图片1)只显示网络图片引用图片处理的java类:package dujun.king.urlgetimage;    import java.io.InputStream;  import java.net.URL;    import android.app.Activity;  import android.graphics.Bitmap;  import android.graphics.BitmapFactory;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.view.Menu;  import android.view.MenuItem;  import android.widget.ImageView;      public class MainActivity extends Activity {        Bitmap bitmap;      ImageView imageview;            Handler handler=new Handler(){          @Override          public void handleMessage(Message msg) {              if (msg.what==0x9527) {                  //显示从网上下载的图片                  imageview.setImageBitmap(bitmap);              }          }                };            @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          imageview=(ImageView)findViewById(R.id.imageView1);               //创建并启动一个新线程用于从网络上下载图片          new Thread(){              @Override              public void run() {                  try {                      //创建一个url对象                      URL url=new URL("http://www.baidu.com/img/bdlogo.png");                      //打开URL对应的资源输入流                      InputStream is= url.openStream();                      //从InputStream流中解析出图片                      bitmap = BitmapFactory.decodeStream(is);                  //  imageview.setImageBitmap(bitmap);                       //发送消息,通知UI组件显示图片                      handler.sendEmptyMessage(0x9527);                      //关闭输入流                      is.close();                  } catch (Exception e) {                      e.printStackTrace();                  }              }                    }.start();            }          @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.main, menu);          return true;      }        @Override      public boolean onOptionsItemSelected(MenuItem item) {          // Handle action bar item clicks here. The action bar will          // automatically handle clicks on the Home/Up button, so long          // as you specify a parent activity in AndroidManifest.xml.          int id = item.getItemId();          if (id == R.id.action_settings) {              return true;          }          return super.onOptionsItemSelected(item);      }  }  

小怪兽爱吃肉

查看API 可通过  JLabel构造函数,也可使用setIcon(Icon icon) 函数,下面给出一个列子!看楼主提问,主要想知道路径怎么得来!可使用this.getClass().getResource("/")获取的是class文件编译后放置的绝对路径。this.getClass().getResource("/images/a.png");编译地址/images/a.png (详见代码20-21)这里图片是放到src --  images -- a.png有问题再追问,good luck!import java.awt.BorderLayout;import java.awt.Container;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;public class MyImages extends JFrame {    /**     * @Fields serialVersionUID : Description     */    private static final long serialVersionUID = 1060195358054863848L;    /**     * 创建一个新的实例 MyImages.     */    public MyImages() {        // TODO Auto-generated constructor stub        Container c = this.getContentPane();        c.setLayout(new BorderLayout());                                                                                                                                                                                                                                                                                                             Icon icon = new ImageIcon(this.getClass().getResource("/images/a.png"));        JLabel label = new JLabel("aaa", icon, JLabel.LEFT);                                                                                                                                                                                                                                                                                                             c.add(label, BorderLayout.CENTER);                                                                                                                                                                                                                                                                                                             this.setSize(500, 500);        this.setVisible(true);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }                                                                                                                                                                                                                                                                                                     public static void main(String[] args) {        new MyImages();    }}

繁星淼淼

读取图片可以有以下两种方法:①:ImageIO.read(new File("这里可以写目录,比如您提到的src/images/某张图片名"));②:new ImageIcon("目录").getImage();这两个方法都返回一个图片对象。可以用一个Image对象接收一下。相对路径是指您所运行的程序的包 所在的文件夹开始的路径。一般来说,上面两种读取方法读取时,是从项目的目录下开始找文件的。所以,您把图片放在src下的images包中,正确的读取方法应该是:Image img=ImageIO.read(new File("src/images/图片名"));或者Image img=new ImageIcon("src/images/图片名").getImage();得到这样一个Image对象后,就可以使用了。
随时随地看视频慕课网APP
我要回答