gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:24:06 25 4
gpt4 key购买 nike

我正在尝试从某人的计算机加载文件并将其作为图标放入标签中。当我尝试运行它时,出现 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 picPanel(朝向底部)不是您从 mouseClicked 中的 setIcon 引用的那个。如果没有类成员 picPanel ,则会导致编译错误,因为在尝试在 中访问它之前,需要声明本地 picPanel鼠标点击

移动 JLabel picPanel = new JLabel("");在添加监听器之前,可能会解决问题。并摆脱 setIcon(null)。但话又说回来,我不知道您的代码中还可能尝试使用 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);
}
}

关于java - 使用 JFileChooser 将图像加载到 JLabel 图标中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26227679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com