java jfilechooser报错,JButton和JFileChooser的错误消息

I want to have a button with a JFileChooser action. This is the code that I wrote:

public class Main {

private static String fullPath;

private JFileChooser inputFile;

public static void main(String args[]) throws FileNotFoundException, IOException {

try {

GridBagConstraints gbc = new GridBagConstraints();

JButton inputButton = new JButton("Browse input file");

myPanel.add(inputButton, gbc);

inputButton.addActionListener(new ActionListener() {

public void ActionPerformed(ActionEvent e) {

JFileChooser inputFile = new JFileChooser();

inputFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

File file1 = inputFile.getSelectedFile();

String fullpathTemp = (String) file1.getAbsolutePath();

fullPath = fullpathTemp;

}

public void actionPerformed(ActionEvent e) {

throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.

}

});

} catch (Exception e) {

System.err.println("Error: " + e.getMessage());

} finally {

}

}

}

but the problem is that when I run it, I got a long error message that is part of:

Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet.

at main.Main$1.actionPerformed(Main.java:200)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)

解决方案

The ActionListener here is explicitly throwing an UnsupportedOperationException. Move the JFileChooser functionality into the ActionListener:

input_button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

JFileChooser inputFile = new JFileChooser();

inputfile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

if (inputfile.showOpenDialog(myFrame) == JFileChooser.APPROVE_OPTION) {

File file1 = inputFile.getSelectedFile();

String fullpathTemp = (String) file1.getAbsolutePath();

...

}

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值