1

私が望むのは、ユーザーがファイルチューザーから画像を選択した後に画像をラベルに表示することです。私は多くのチュートリアルに従いました。 -display.htmlイメージ名をハードコーディングするとうまくいきましたが、本当に必要なのは、ユーザーがファイルチューザーから選択したものを表示することです。この int retV を試しました。ファイル file=null;

    if (fc== null)
        fc=new JFileChooser();

    fc.addChoosableFileFilter(new ImageFilter());
    fc.setAcceptAllFileFilterUsed(false);

    retV =fc.showOpenDialog(GUI.this);
    if (retV == JFileChooser.APPROVE_OPTION)
         file = fc.getSelectedFile();
    infoText.append("-The image : "+file.getAbsolutePath()+"\nwill be conveted to PNG format becuase it's a lossless\n compression.\n");

    ImageIcon icon= new ImageIcon(file.getAbsolutePath());
    img1=new JLabel(icon);

しかし、うまくいきませんでした。

どんな助けでも本当に感謝しています

4

3 に答える 3

2

File別のオプションは、選択したものをに変換することURLです:

selectedFile.toURI().toURL()

SSCCE :

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class TrivialImageViewer implements Runnable {

    private static final Font DEMO_FONT = new Font("Arial", Font.BOLD, 20);

    private final JFrame frame;
    private final JFileChooser fileChooser;
    private final JLabel iconLabel;

    private final class SelectImageAction extends AbstractAction {
        private static final long serialVersionUID = 1L;

        private SelectImageAction(final String name) {
            super(name);
        }

        @Override
        public void actionPerformed(final ActionEvent e) {

            final int option = fileChooser.showOpenDialog(frame);
            if (option != JFileChooser.APPROVE_OPTION) {
                return;
            }

            final File selectedFile = fileChooser.getSelectedFile();

            URL url;
            try {
                url = selectedFile.toURI().toURL();
            } catch (final MalformedURLException e1) {
                throw new RuntimeException(e1);
            }

            final ImageIcon icon= new ImageIcon(url);
            iconLabel.setText("");
            iconLabel.setIcon(icon);
        }
    }

    public TrivialImageViewer() {
        frame = new JFrame();
        frame.setTitle("Trivial Swing ImageViewer");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        iconLabel = new JLabel("Please select an image.");
        iconLabel.setFont(DEMO_FONT);
        iconLabel.setHorizontalAlignment(SwingConstants.CENTER);

        fileChooser = new JFileChooser();

        frame.add(iconLabel, BorderLayout.CENTER);
        frame.add(new JButton(new SelectImageAction("Select Image...")), BorderLayout.SOUTH);
    }

    @Override
    public void run() {
        frame.setBounds(16, 16, 640, 480);
        frame.setVisible(true);
    }

    public static void main(final String[] args) throws Exception {
                    // Use EventQueue.invokeLater in serious apps...
        final TrivialImageViewer application = new TrivialImageViewer();
        application.run();
    }
}

これを教室の外で使用する場合は、適切なリソース管理を実装することを忘れないでください。

于 2013-06-07T16:36:52.620 に答える