actionPerformed出错?

时间:2014-01-18 11:22:43

标签: java swing actionlistener ioexception

我正在尝试创建一个用于导入CSV文件的按钮,我收到此错误:

actionPerformed(java.awt.event.ActionEvent) in  cannot implement 
    actionPerformed(java.awt.event.ActionEvent) in 
    java.awt.event.ActionListener; overridden method does not throw 
    java.io.IOException

public void actionPerformed(java.awt.event.ActionEvent evt) throws IOException {  
1 error

代码

import java.io.*;
import java.util.Arrays;
public class NewJFrame extends javax.swing.JFrame {

public NewJFrame() {
    initComponents();
}

private void initComponents() {
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    jButton1.setText("jButton1");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) throws IOException {
            jButton1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(130, 130, 130)
            .addComponent(jButton1)
            .addContainerGap(197, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(130, 130, 130)
            .addComponent(jButton1)
            .addContainerGap(147, Short.MAX_VALUE))
    );
    pack();
}

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) throws IOException {                                         
     String xfileLocation;

    xfileLocation="C:\\tra\\data1.txt";

    BufferedReader CSVFile = new BufferedReader(new FileReader(xfileLocation));
    try{
    String dataRow = CSVFile.readLine(); // Read the first line of data.
    // The while checks to see if the data is null. If it is, we've hit
    //  the end of the file. If not, process the data.
    while (dataRow != null){
        String[] dataArray = dataRow.split(",");
        for (String item:dataArray) { System.out.print(item + "\t"); }
        System.out.println(); // Print the data line.
        dataRow = CSVFile.readLine(); // Read next line of data.
    }

    // Close the file once all data has been read.
    CSVFile.close();

    // End the printout with a blank line.
    System.out.println();


     }
    catch(IOException e){
     }

}  

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}     

private javax.swing.JButton jButton1;
}                               

1 个答案:

答案 0 :(得分:3)

是的 - 看看declaration of the method you're implementing

void actionPerformed(ActionEvent e)

未声明抛出任何已检查的异常。因此,您不能添加已检查的异常,例如带有IOException子句的throws。您必须在侦听器中处理异常,或将其转换为未经检查的异常并抛出该异常。

(你的异常处理已经远非理想了 - 在方法中,你抓住IOException并完全静默地吞下它......在这种情况下无法关闭文件。你应该在finally块中关闭阅读器如果您正在使用Java 7,或者使用try-with-resources语句,并且至少记录异常,可能会告诉用户它。继续好像什么都没有出错就是真的坏主意。)