小编use*_*295的帖子

显示来自文件选择器的图像

我在显示从我创建的文件选择器获得的图像时遇到问题。你能给我建议吗?图像被创建为缓冲图像。

这是我的代码:

public void actionPerformed(ActionEvent e)
{
    if (e.getSource().getClass().getName().contains("JMenuItem")) 
    {
        if (e.paramString().contains("Load")) {
            JFileChooser fc = new JFileChooser();
            fc.setCurrentDirectory(new File("."));
            int retVal = fc.showOpenDialog(null);
            if (retVal == 0) 
            {
                File file = fc.getSelectedFile();
                try {
                image = ImageIO.read(file);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

下面是显示代码:

public void paint(Graphics g){  
    super.paintComponents(g);
    g.drawImage(getIconImage(), 0, 0, control);
    g.drawImage( image, 0, 0,null);
    repaint();
}
Run Code Online (Sandbox Code Playgroud)

java swing image

2
推荐指数
1
解决办法
3176
查看次数

标签 统计

image ×1

java ×1

swing ×1