单击按钮时运行方法

时间:2016-07-02 05:45:54

标签: java methods

单击按钮(重置按钮)时,我需要运行某个功能。 我收到一个错误:"非法开始表达"需要帮助:

private void resetActionPerformed(java.awt.event.ActionEvent evt) {                                      

    String sDay = day.getText();
    String sName = name.getText();
    String sCash = cash.getText();
    String sReason = reason.getText();
    String sCourse = courses.toString();

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

    String fileName = "E:\\Java Workspace\\file" + day + ".xls";
    WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
    WritableSheet sheet = workbook.createSheet(name, 0);

    Label label1 = new Label(0, 0, "A label word record");
    sheet.addCell(label1);

    jxl.write.Number number = new jxl.write.Number(0, 1, 3.1459);
    sheet.addCell(number);

    workbook.write();
    workbook.close();
}
// TODO add your handling code here:
}

1 个答案:

答案 0 :(得分:1)

以下是您的代码的正确版本:

public class FooExample {

    private void resetActionPerformed(java.awt.event.ActionEvent evt) {                                      

        String sDay = day.getText();
        String sName = name.getText();
        String sCash = cash.getText();
        String sReason = reason.getText();
        String sCourse = courses.toString();

    }  // this bracket was missing in the original code 

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

        try {
            String fileName = "E:\\Java Workspace\\file" + day + ".xls";
            WritableWorkbook workbook = Workbook.createWorkbook(new File(fileName));
            WritableSheet sheet = workbook.createSheet(name, 0);

            Label label1 = new Label(0, 0, "A label word record");
            sheet.addCell(label1);

            jxl.write.Number number = new jxl.write.Number(0, 1, 3.1459);
            sheet.addCell(number);

            workbook.write();
            workbook.close();
        } catch(Exception ex){ // the fix
            System.out.println("Error " + ex.getMessage());
            //TODO handle Exception as you need
        }
  }
}

您应该使用try catch块来处理已检查和未经检查的例外。我把它添加到代码中。请注意TODO应添加异常处理代码的地方。