如何处理摆动中图像显示的错误文件选择 [英] how to handle bad file selection for image display in swing

查看:95
本文介绍了如何处理摆动中图像显示的错误文件选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Swing并编写了一个应用程序,允许用户选择一个图像文件并将其显示在 JPanel 上。它有效,但我想处理的情况

I am learning Swing and wrote an app that lets user select an image file and displays it on a JPanel. It works, but I want to handle situations when


  1. 用户没有选择任何文件

  2. 用户选择非图像文件

在这些情况下,我想清除 JPanel 并在文本区域显示错误消息。

In these cases I want to clear the JPanel and show an error message on a text area.

我试图按照以下方式执行此操作。但我不确定这是否是正确执行此操作的方法。想要你的建议。

I tried to do this as below.But I am not sure if this is the way to do this properly.I would like your suggestions.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

class Display extends JFrame {

    private MyImagePanel canvas;
    private JButton okbutton;
    private JTextArea result;
    private JFileChooser filechooser;
    private static Insets insets = new Insets(0, 0, 0, 0);

    public Display(String name) {
        super(name);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());
        addWidgets();
        showGUI();
    }

    private void showGUI() {
        this.pack();
        this.setVisible(true);
    }

    private void addWidgets() {
        canvas = new MyImagePanel();
        okbutton = new JButton("OK");
        filechooser = new JFileChooser("Select imagefile");
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Images", "jpg", "JPG", "GIF", "gif", "JPEG", "png", "PNG");
        filechooser.setFileFilter(filter);
        filechooser.setControlButtonsAreShown(false);
        result = new JTextArea(10, 10);
        addComponent(filechooser, 0, 0, 2, 4, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        addComponent(canvas, 2, 0, 2, 2, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        addComponent(result, 2, 2, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
        addComponent(okbutton, 3, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE);
    }

    public void addOKButtonListener(ActionListener okl) {
        okbutton.addActionListener(okl);
    }

    public void displayResult(String msg) {
        result.setText(msg);
    }

    public void clearResultField() {
        result.setText("");
    }

    public void displayImage(String imagefilename) {
        canvas.setImage(imagefilename);
    }

    public String getSelectedFile() {
        java.io.File selectedFile = filechooser.getSelectedFile();
        String filePathName = "";
        if (selectedFile == null) {
            result.setText("select a file");
        } else {
            filePathName = selectedFile.getPath();
        }
        return filePathName;
    }

    public void addComponent(Component component, int gridx, int gridy,
        int gridwidth, int gridheight, int anchor, int fill) {
        GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
            gridwidth, gridheight, 1.0, 1.0, anchor, fill, insets, 0, 0);
        this.add(component, gbc);
    }
}//end class Display

class MyImagePanel extends JPanel {

    private BufferedImage bi;

    public MyImagePanel() {
        super();
        bi = null;
    }

    public void setImage(String imagefilename) {
        try {
            bi = ImageIO.read(new File(imagefilename));
            this.setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
        } catch (Exception e) {
            bi = null;
        }
        this.revalidate();
        this.repaint();
    }

    @Override
    public void paintComponent(Graphics g) {
        // clear the background
        g.setColor(getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());
        if (bi != null) {
            // draw the image
            g.drawImage(bi, 0, 0, null);
        }
    }
}

class GUIController {

    private Display display;

    public GUIController(Display d) {
        display = d;
        display.addOKButtonListener(new OKButtonListener());
    }

    class OKButtonListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            display.clearResultField();//make result field blank
            String fileselection = display.getSelectedFile();
            if (fileselection.length() > 0) {
                display.displayImage(fileselection);
            } else {
                display.displayResult("actionPerformed:no file selected");
                display.displayImage("");
            }
        }
    }
}

public class ImageDisplay {

    public static void main(String[] args) {
        Display d = new Display("image demo");
        GUIController ctrl = new GUIController(d);
    }
}


推荐答案

drawImage()方法rel =nofollow noreferrer> java.awt.Graphics 如果 img 。因此,将图像设置为 null 就足够了。您正在明确清除背景,但 super.paintComponent(g)是一种将面板清除为背景颜色的替代方法。

The several drawImage() methods in java.awt.Graphics do "nothing if img is null." As a result, setting the image to null is sufficient. You're clearing the background explicitly, but super.paintComponent(g) is an alternative that clears the panel to the background color.

附录:您可能还想研究文章中的示例 如何使用文件选择器 使用图像

Addendum: You may also want to study the examples found in the articles How to Use File Choosers and Working with Images.

附录:我使用了不同的布局,并将图像添加到 JScrollPane 。我还有 setImage()返回一个结果让显示知道发生了什么。

Addendum: I used a different layout and added the image to a JScrollPane. I also had setImage() return a result to let the Display know what happened.

附录:这个更新,更简单的修订版扩展了 JFileChooser 直接处理批准和取消。

Addendum: This newer, simpler revision extends JFileChooser to handle approve and cancel directly.

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

/** @see https://stackoverflow.com/questions/4053090 */
public class ImageDisplay extends JFrame {

    private static final String title = "Select a file";
    private MyImagePanel imagePanel = new MyImagePanel();
    private JLabel result = new JLabel(title, JLabel.CENTER);
    private MyChooser fileChooser = new MyChooser();

    public ImageDisplay(String name) {
        super(name);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.addWidgets();
        this.pack();
        this.setVisible(true);
    }

    private void addWidgets() {
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
            "Images", "jpg", "JPG", "GIF", "gif", "JPEG", "png", "PNG");
        fileChooser.setFileFilter(filter);
        this.add(fileChooser, BorderLayout.WEST);
        this.add(new JScrollPane(imagePanel), BorderLayout.CENTER);
        this.add(result, BorderLayout.SOUTH);
    }

    class MyChooser extends JFileChooser {

        @Override
        public void approveSelection() {
            File f = fileChooser.getSelectedFile();
            if (imagePanel.setImage(f)) {
                result.setText(f.getName());
            } else {
                result.setText(title);
            }
        }

        @Override
        public void cancelSelection() {
            imagePanel.setImage(null);
            result.setText(title);
        }
    }

    class MyImagePanel extends JPanel {

        private BufferedImage bi;

        public MyImagePanel() {
            this.setPreferredSize(new Dimension(500, 700));
        }

        /** Return true if read() succeeded. */
        public boolean setImage(File f) {
            try {
                bi = ImageIO.read(f);
            } catch (Exception e) {
                bi = null;
            }
            if (bi != null) {
                setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
            }
            this.revalidate();
            this.repaint();
            return bi != null;
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(bi, 0, 0, null);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new ImageDisplay("Image Demo").setVisible(true);
            }
        });
    }
}

这篇关于如何处理摆动中图像显示的错误文件选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆