gpt4 book ai didi

java - actionPerformed 出错?

转载 作者:行者123 更新时间:2023-11-30 08:26:53 29 4
gpt4 key购买 nike

我正在尝试创建一个用于导入 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;
}

最佳答案

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

void actionPerformed(ActionEvent e)

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

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

关于java - actionPerformed 出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21203400/

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