在两个事件处理程序之间访问文件

时间:2016-03-06 05:56:06

标签: java

mknj,n。,, mn,mn m,n m,mn jjj

1 个答案:

答案 0 :(得分:0)

在选择Excel文件的方法中,您引用了pane

pane.add(label1, 1, 0);

基本上你可以为文件做同样的事情,但是你必须为这个案例添加一些特殊处理,文件没有被初始化,就像打开它们时那样:

public void start(Stage PrimaryStage) {
    File file1, file2;
    ...
    input2.setOnAction(
    new EventHandler<ActionEvent>() {
        public void handle(final ActionEvent e) {
        file1 = inputFile.showOpenDialog(PrimaryStage);
        Label label2 = new Label(file1.getPath());
        pane.add(label2, 1, 1);                         
        }           
    });
    output1.setOnAction(
    new EventHandler<ActionEvent>() {
        public void handle(final ActionEvent e) {
        file2 = outputFile.showDialog(PrimaryStage);
        System.out.print(file2.getPath());
        Label label3 = new Label(file2.getPath());
        pane.add(label3, 1, 2);                         
        }           
    });
    ...
    execute.setOnAction(
        new EventHandler<ActionEvent>() {
            public void handle(final ActionEvent e) {
                if (file1 != null) {
                    XSSFWorkbook wb = new XSSFWorkbook(file1);
                }
                if (file2 != null) {
                    XSSFWorkbook wb2 = new XSSFWorkbook(file2);
                }
                ...
            }           
    });

通过该更改,您已在方法范围上定义了file1file2,并可以在lambda中访问它。

相关问题