当前位置: 代码迷 >> J2SE >> Java图片显示不了解决方法
  详细解决方案

Java图片显示不了解决方法

热度:17   发布时间:2016-04-24 00:57:35.0
Java图片显示不了
代码如下:
Java code
import javax.imageio.ImageIO;import javax.swing.*;import java.awt.*;import java.awt.image.*;import java.awt.event.*;import java.io.*;public class AppPanel extends JPanel {    //variables and components==========================    private JPanel imageBox;    private JPanel toolPane;    private JLabel counterLabel;    private int counter;    private boolean showWholeImage;    private JButton selectButton;    private JButton breakButton;    private JButton viewImageButton;    private Image[] images;    private Image wholeImage;    private JFileChooser chooser;    //constructor=============================    public AppPanel(){        //initial variables        chooser = new JFileChooser();        images=new BufferedImage[24];        wholeImage=null;        showWholeImage = true;        counter=0;        setLayout(new BorderLayout());        //initial components        imageBox = new JPanel();        imageBox.setBackground(Color.WHITE);        toolPane = new JPanel();        counterLabel = new JLabel("操作次数:"+counter);        viewImageButton = new JButton("显示原图");        selectButton = new JButton("选择图片");        selectButton.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent event){                if(JFileChooser.APPROVE_OPTION==chooser.showOpenDialog(null)){                    try{                        wholeImage = ImageIO.read(chooser.getSelectedFile());                        showWholeImage=true;                        repaint();                                            }                    catch(Exception e){                        JOptionPane.showMessageDialog(null, "打开文件失败");                    }                }            }        });        breakButton = new JButton("打乱图片");        toolPane.add(counterLabel);        toolPane.add(selectButton);        toolPane.add(breakButton);        toolPane.add(viewImageButton);        add(toolPane,BorderLayout.SOUTH);        add(imageBox);    }    //paintComponents    public void paintComponent(Graphics g){        super.paintComponent(g);        Graphics g2d = imageBox.getGraphics();        if(showWholeImage && wholeImage!=null){            g2d.drawImage(wholeImage,0,0,imageBox);        }        else{            int x =0,y=0;            for(int i=0;i<24;i++){                g2d.drawImage(images[i],x, y, null);            }        }    }}

难道不是每次选择图片后,调用repaint直接将图片绘制到imageBox上吗,为什么总是显示不了?

------解决方案--------------------
应该没啥问题啊。jar包用的对不对?
------解决方案--------------------
给楼主顶下
------解决方案--------------------
不知道是不是你图片位置没有放对 我是随便说说的哈
------解决方案--------------------
试了一下,换成JLabel好使。
JLabel lable = new JLabel();
lable.setIcon(new ImageIcon(wholeImage));
  相关解决方案