导航:首页 > 编程语言 > java本地图片显示

java本地图片显示

发布时间:2023-08-14 07:58:19

A. 请教如何用java语言读取jpg图片,并显示

1、获取文件夹的路径 2、得到文件夹中的有图片的名称,可以存到数组或者集合中 3、你再到jsp页面做显示, 4、下面是获取路径和文件名的代码,前台显示的代码自己写 String path = 文件夹路径; String names = ""; try { File f = new File(path)

B. 在窗体中,java显示图片怎么做

下面是一个JAVA显示图片的例子,请参考:
package com.tarena.java;
import t.Image;
import .File;
import .IOException;
import ageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
/**
* 加载显示图象,需要JDK1.5或以上
*/
public class showtu extends JFrame {
public showtu(String bmpFile) {
Image image = null;
try {
image = ad(new File(bmpFile));
} catch (IOException ex) {
}
JLabel label = new JLabel(new ImageIcon(image));
add(label);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
}

public static void main(String[] args){
final String fileName = "F:\\456备用\\亮个相.JPG"; //换成你要显示的图片

vokeLater(new Runnable(){
public void run(){
new showtu(fileName).setVisible(true);
}
});
}
}

C. java怎么显示本地图片

在面板上搞一个和面板一样大的JLabel
然后,通过JFileChooser获得路径,利用这个图片的路径,构建一个ImageIcon
最后,根据这个ImageIcon去给JLabel对象setIcon(ImageIcon对象);

具体地:
1.panel.add(label,BorderLayout.CENTER);
2.ImageIcon icon = new ImageIcon(url);
3.label.setIcon(icon);

下面的代码你把 .JPG改成BMP试试看,O(∩_∩)O~
package com.shlq.sample;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ImagePane extends JPanel
{
JLabel jl = null;
ImageIcon img = null;

public ImagePane()
{
img = new ImageIcon( "E:\\Picture\\1.jpg ");
jl = new JLabel(img);
this.setLayout(new BorderLayout());
this.add(jl, BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame test = new JFrame( "Image Pane ");
test.getContentPane().add(new ImagePane());
test.pack();
test.setVisible(true);
test.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}

D. java如何读取文件夹中的图片并在界面显示

下面给你提供一个实现,该实现采用了代理模式。这个实现包含两个文件,分别是Client.java和ImageIcoProxy.java,ImageIcoProxy.java负责了图片的延迟加载,你可以修改为不延迟即可。

Client.java的代码为:
import java.awt.Graphics;
import java.awt.Insets;

import javax.swing.Icon;
import javax.swing.JFrame;

public class Client extends JFrame {
private static int IMG_WIDTH = 510;
private static int IMG_HEIGHT = 317;
private Icon imgProxy = null;
public static void main(String[] args) {
Client app = new Client();
app.setVisible(true);
}

public Client() {
super("Virture Proxy Client");
imgProxy = new ImageIcoProxy("D:/test.jpg", IMG_WIDTH, IMG_HEIGHT);
this.setBounds(100, 100, IMG_WIDTH + 10, IMG_HEIGHT + 30);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
super.paint(g);
Insets insets = getInsets();
imgProxy.paintIcon(this, g, insets.left, insets.top);
}
}

ImageIcoProxy.java的代码为:
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.SwingUtilities;

public class ImageIcoProxy implements Icon {

private ImageIcon realIcon = null;
private String imgName;
private int width;
private int height;
boolean isIconCreated = false;
public ImageIcoProxy(String imgName, int width, int height) {
this.imgName = imgName;
this.width = width;
this.height = height;
}

public int getIconHeight() {
return realIcon.getIconHeight();
}

public int getIconWidth() {
return realIcon.getIconWidth();
}

public void paintIcon(final Component c, Graphics g, int x, int y) {
if (isIconCreated) {
//已经加载了图片,直接显示
realIcon.paintIcon(c, g, x, y);
g.drawString("Just Test", x + 20, y + 370);
} else {
g.drawRect(x, y, width-1, height-1);
g.drawString("Loading photo...", x+20, y+20);
synchronized(this) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
try {
Thread.currentThread().sleep(2000);
realIcon = new ImageIcon(imgName);
isIconCreated = true;
} catch (Exception e) {
e.printStackTrace();
}
c.repaint();
}

}
);
}
}
}

}

阅读全文

与java本地图片显示相关的资料

热点内容
安卓手机为什么没有桌面插件 浏览:346
鼎新app是什么 浏览:113
程序员和施工员哪个有前途 浏览:614
浙江百兆服务器云空间 浏览:827
php和html5学哪个比较好 浏览:388
瑜伽pdf下载地址 浏览:252
小花猫app改名叫什么 浏览:837
编程器固件修改 浏览:523
沈阳技校有程序员吗 浏览:300
各行app的基金有什么不一样吗 浏览:62
python编译器怎么装 浏览:73
新浪php笔试题 浏览:138
新时达服务器如何自学习 浏览:27
程序中什么命令用来输入单字符 浏览:59
php是否有数据 浏览:141
虚拟云服务器是什么意思 浏览:279
vb是编译性语言吗 浏览:209
json格式用什么编译器 浏览:319
word转pdf代码 浏览:802
单片机中如何编程 浏览:739