我试图将具有我计算机上文件路径文本的jLabel变为可以按下并使用方法的“链接”
java.awt.Desktop.getDesktop()。open(selectedFile);
当我按下它时,出现异常“线程“ AWT-EventQueue-0”中的异常java.lang.NullPointerException”
按jLabel的名称是“标签”。
我该如何解决?
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser file = new JFileChooser();
file.setCurrentDirectory(new
File(System.getProperty("user.home")));
FileNameExtensionFilter filter = new
FileNameExtensionFilter("*.Images", "jpg", "gif", "png");
file.addChoosableFileFilter(filter);
int result = file.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
File selectedFile = file.getSelectedFile();
String path = selectedFile.getAbsolutePath();
label.setText(path);
}
}
});
class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == emailknapp) {
String message = showMessage.getText();
String user = "mail@infbook.page";
String pass = "Infbook2019";
SendMail.send(email, "SupportChat conversation", message,
user, pass);
}
}
}
label.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
try {
java.awt.Desktop.getDesktop().open(selectedFile);
} catch (IOException ex) {
Logger.getLogger(CustomerWindow.class.getName()).log(Level.SEVERE, null,
ex);
}
}
});