在一个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();
}
}

}
}

解决方案 »

  1.   


    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();
    }
    } }
    }
      

  2.   

    你的结构不是很提倡了,只有极个别的时候,需要UI和线程一起互相操作。
     你可以看看Swing和Thread的部分,这样肯定是不行的。
      

  3.   

    太谢谢了,这个事作业,平时专注于j2ee