java代码导入图片 java如果导入图片到项目

怎样在java里用URL引入图片引入图片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("");
//打开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);
}
}
JAVA程序如何插入图片图片名字是img1.JPG,你放到本程序的目录下面就好了 。
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
public class TestMenu1 extends JFrame{
private JTextArea textArea=new JTextArea();
private JMenuBar menuBar=new JMenuBar();
private JMenu fileMenu=new JMenu("文件");
private JMenu viewMenu=new JMenu("视图");
private JMenu toolMenu=new JMenu("工具栏");
private JMenuItem[] fileItem={new JMenuItem("新建"),new JMenuItem("打开"),new JMenuItem("保存"),new JMenuItem("退出")};
private JMenuItem[] viewItem={new JMenuItem("普通"),new JMenuItem("页面")};
private JCheckBoxMenuItem[] toolItem={new JCheckBoxMenuItem("常用"),new JCheckBoxMenuItem("绘图"),new JCheckBoxMenuItem("符号栏")};
private JPanel jPanel1;
private JLabel jLabel;
private Imageimage;
private ImageIconimageIcon ;
public TestMenu1(String title){
super(title);
jPanel1=new JPanel();
image = Toolkit.getDefaultToolkit().getImage("img1.JPG");
jLabel = new JLabel();
imageIcon = new ImageIcon(image);
jLabel.setIcon(imageIcon);

推荐阅读