动态显示图片

sj307639429 2009-11-16 07:53:19
在一个JFrame里用JLable显示图片,图片路径由监听按钮时通过FileDialog取得。路径去的无误但图片为什么不显示,高手们帮忙啊。源代码如下:
package org.jun.image;
import java.awt.*;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ShowFileDialog extends JFrame{
static String filePath = null;//图片路径
Image image =null;
boolean thread = false; //线程控制
static ShowFileDialog dialog;

public ShowFileDialog() {
super("图片查看");
}

public void launchFrame() {
JButton jButt = new JButton("图片路径");

setLocation(200,100);
setSize(600,400);
add(jButt,BorderLayout.SOUTH); //添加按钮

dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jButt.addMouseListener(new MouseListener() { //监听Mouse点击事件
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) { //鼠标放开时, 开始这里很正常,后来调试时不知道改了哪,只要选了一次文件,就会马上弹出个FileDialog的对话框
FileDialog fd = new FileDialog(dialog);

fd.setSize(400, 300);
fd.setVisible(true);

filePath = fd.getDirectory() + fd.getFile();
System.out.println(filePath);
thread = true;

dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
}//end of class
);

this.setVisible(true);
}
public static void main(String[] args) {
dialog = new ShowFileDialog();
dialog.starts();
}

public void starts() {
dialog.launchFrame();
new Thread(new ImageThread("d")).start();
}

class ImageThread implements Runnable { //另起线程,当filePath改变时显示图片
private String s;
public ImageThread (String s) {
this.s = s;
}
public void run() {
while(thread) {
Image image =null;
try{
image=ImageIO.read(new File(filePath));
}catch(IOException ex){
System.out.println("文件路径错误");
}
JLabel label =new JLabel(new ImageIcon(image));
dialog.getContentPane().add(label, BorderLayout.NORTH);
dialog.setVisible(true);
pack();
}
}

}
}
...全文
83 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sj307639429 2009-11-16
  • 打赏
  • 举报
回复
晕,发重了,我的分~~~
sj307639429 2009-11-16
  • 打赏
  • 举报
回复
晕,发重了,我的分~~~
zl3450341 2009-11-16
  • 打赏
  • 举报
回复
看楼上的代码
healer_kx 2009-11-16
  • 打赏
  • 举报
回复
大致改了一下,很多类名什么的没有动,虽然不合对象要表达的意义了。

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;

public class ShowFileDialog extends JFrame {
static String filePath = null;// 图片路径
Image image = null;
boolean thread = false; // 线程控制
static ShowFileDialog dialog;
private JLabel label = null;

public ShowFileDialog() {
super("图片查看");
}

public void launchFrame() {
JButton jButt = new JButton("图片路径");

setLocation(200, 100);
setSize(600, 400);
add(jButt, BorderLayout.SOUTH); // 添加按钮
label = new JLabel();
label.setBackground(Color.BLACK);
label.setBounds(new Rectangle(0, 0, 100, 100));
dialog.add(label, BorderLayout.NORTH);
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

jButt.addMouseListener(new MouseListener() { // 监听Mouse点击事件

public void mouseReleased(MouseEvent e) { // 鼠标放开时,
// 开始这里很正常,后来调试时不知道改了哪,只要选了一次文件,就会马上弹出个FileDialog的对话框
FileDialog fd = new FileDialog(dialog);

fd.setSize(400, 300);
fd.setVisible(true);

filePath = fd.getDirectory() + fd.getFile();
System.out.println(filePath);
new ImageThread("d").run();

dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

}// end of class
);

this.setVisible(true);
}

public JLabel getLabel() {
return label;
}

public static void main(String[] args) {
dialog = new ShowFileDialog();
dialog.starts();
}

public void starts() {
dialog.launchFrame();

}



class ImageThread implements Runnable { // 另起线程,当filePath改变时显示图片
private String s;

public ImageThread(String s) {
this.s = s;
}

public void run() {
if (true && filePath != null) {
Image image = null;
try {
image = ImageIO.read(new File(filePath));
} catch (IOException ex) {
System.out.println("文件路径错误");
}
getLabel().setIcon(new ImageIcon(image));

dialog.setVisible(true);
pack();
}
}

}
}
healer_kx 2009-11-16
  • 打赏
  • 举报
回复
请不要在线程里面操作UI。这个是原则问题。

62,614

社区成员

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

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