繁体   English   中英

使用JFileChooser将图像加载到JLabel图标中

[英]Using JFileChooser to load an Image into a JLabel Icon

我试图从某人的计算机加载文件,然后将其作为图标放入标签中。 当我尝试运行它时,出现NullPointer错误。 当我到达setIcon代码时,它会中断

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                TheChooser frame = new TheChooser();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public TheChooser() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton searchButton = new JButton("Search Picture");
    searchButton.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent arg0) {
            FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
            fc.setFileFilter(filter);
            int response = fc.showOpenDialog(null);
        try{
            if (response == JFileChooser.APPROVE_OPTION) {
                String pathName = fc.getSelectedFile().getPath();
                JOptionPane.showMessageDialog(null, pathName);
                ImageIcon icon = new ImageIcon(pathName);
                picPanel.setIcon(icon);
            } else {
                JOptionPane.showMessageDialog(null, "Feel Free to Look Later");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
    });
    searchButton.setBounds(141, 11, 139, 23);
    contentPane.add(searchButton);

    JLabel picPanel = new JLabel("");
    picPanel.setIcon(null);
    picPanel.setBounds(10, 58, 414, 192);
    contentPane.add(picPanel);
}

那怎么办呢?

我尝试在下面进行修复,但仍然出现了很多错误

            java.lang.NullPointerException
                at chooser.TheChooser$2.mouseClicked(TheChooser.java:70)
                at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
                at java.awt.Component.processMouseEvent(Unknown Source)
                at javax.swing.JComponent.processMouseEvent(Unknown Source)
                at java.awt.Component.processEvent(Unknown Source)
                at java.awt.Container.processEvent(Unknown Source)
                at java.awt.Component.dispatchEventImpl(Unknown Source)
                at java.awt.Container.dispatchEventImpl(Unknown Source)
                at java.awt.Component.dispatchEvent(Unknown Source)
                at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
                at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
                at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
                at java.awt.Container.dispatchEventImpl(Unknown Source)
                at java.awt.Window.dispatchEventImpl(Unknown Source)
                at java.awt.Component.dispatchEvent(Unknown Source)
                at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
                at java.awt.EventQueue.access$200(Unknown Source)
                at java.awt.EventQueue$3.run(Unknown Source)
                at java.awt.EventQueue$3.run(Unknown Source)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
                at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
                at java.awt.EventQueue$4.run(Unknown Source)
                at java.awt.EventQueue$4.run(Unknown Source)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
                at java.awt.EventQueue.dispatchEvent(Unknown Source)
                at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                at java.awt.EventDispatchThread.run(Unknown Source)
            java.lang.NullPointerException
                at chooser.TheChooser$2.mouseClicked(TheChooser.java:70)
                at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
                at java.awt.Component.processMouseEvent(Unknown Source)
                at javax.swing.JComponent.processMouseEvent(Unknown Source)
                at java.awt.Component.processEvent(Unknown Source)
                at java.awt.Container.processEvent(Unknown Source)
                at java.awt.Component.dispatchEventImpl(Unknown Source)
                at java.awt.Container.dispatchEventImpl(Unknown Source)
                at java.awt.Component.dispatchEvent(Unknown Source)
                at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
                at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
                at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
                at java.awt.Container.dispatchEventImpl(Unknown Source)
                at java.awt.Window.dispatchEventImpl(Unknown Source)
                at java.awt.Component.dispatchEvent(Unknown Source)
                at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
                at java.awt.EventQueue.access$200(Unknown Source)
                at java.awt.EventQueue$3.run(Unknown Source)
                at java.awt.EventQueue$3.run(Unknown Source)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
                at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
                at java.awt.EventQueue$4.run(Unknown Source)
                at java.awt.EventQueue$4.run(Unknown Source)
                at java.security.AccessController.doPrivileged(Native Method)
                at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
                at java.awt.EventQueue.dispatchEvent(Unknown Source)
                at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
                at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
                at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
                at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
                at java.awt.EventDispatchThread.run(Unknown Source)

我的猜测是,您将picPanel声明为类成员。 JLabel picPanelJLabel picPanel不是您在mouseClickedsetIcon引用的setIcon 如果没有类成员picPanel ,则将导致编译错误,因为在尝试通过mouseClicked访问它之前,需要声明本地picPanel

移动JLabel picPanel = new JLabel(“”); 在添加侦听器之前,可能会解决此问题。 并摆脱setIcon(null) 但是话又说回来,我不知道您在代码中picPanel使用picPanel ,因此您可能想要摆脱本地声明,因为它使类成员picPanel阴影。 所以基本上不是

JLabel picPanel = new JLabel();
searchButton.addMouseListener(new MouseAdapter()

采用

picPanel = new JLabel();
searchButton.addMouseListener(new MouseAdapter()

这是完整程序中的修复程序

public class TheChooser extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TheChooser frame = new TheChooser();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    private JPanel contentPane;
    private JFileChooser fc = new JFileChooser();
    private JLabel picPanel;

    public TheChooser() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        //JLabel picPanel = new JLabel("");

        picPanel = new JLabel();
        JButton searchButton = new JButton("Search Picture");
        searchButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
                fc.setFileFilter(filter);
                int response = fc.showOpenDialog(null);
                try {
                    if (response == JFileChooser.APPROVE_OPTION) {
                        String pathName = fc.getSelectedFile().getPath();
                        JOptionPane.showMessageDialog(null, pathName);
                        ImageIcon icon = new ImageIcon(pathName);
                        picPanel.setIcon(icon);
                    } else {
                        JOptionPane.showMessageDialog(null, "Feel Free to Look Later");
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        searchButton.setBounds(141, 11, 139, 23);
        contentPane.add(searchButton);
        picPanel.setBounds(10, 58, 414, 192);
        contentPane.add(picPanel);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM