打开jar文件时,Java FX拖放不起作用

时间:2018-12-05 22:40:28

标签: java javafx netbeans

我对JavaFX相当陌生,目前正在开发一个相当简单的程序。用户将一个文件夹拖放到我的应用程序中,然后它将生成该文件夹的.zip版本。我正在使用JavaFX使其成为用户友好的UI。

我的问题是,当我在Netbeans 8.2上运行项目时,程序执行了应做的事情。但是,当我在“构建并清理”之后运行jar文件时,将其拖放到我的应用中的文件夹不允许这样做。

我已经阅读过有关NetBeans的本机打包的信息,但是由于某种原因,它会产生相同的结果。

这是我的代码:

public class FXMLDocumentController implements Initializable {

@FXML
private Label label,fileName;
@FXML
private Button button;
@FXML
private ImageView imageView;

private File file = null;
private boolean isFolder = false;
private  String locationStr= "\\html5\\data\\js\\data.js";



@FXML
private void handleButtonAction(ActionEvent event) throws IOException{
    System.out.println("handleButtonAction");
    //createJSFile();
    File newDir= copyDir(file);
    String newDirStrLocation = newDir.getAbsolutePath();
    String newDirContents = "";

    BufferedReader br = checkDataJSFile(newDirStrLocation);

    newDirContents=br.readLine();
    System.out.println(newDirContents);
    createJSFile(newDirContents, newDirStrLocation+""+locationStr);

    File assets = new File("assets");

    FileUtils.copyDirectory(assets, newDir);

    Compression.zipFile(newDirStrLocation, newDirStrLocation+".zip", true);

    newDir.delete();


}

@Override
public void initialize(URL url, ResourceBundle rb) {
    button.setVisible(false);
}    

@FXML
private void handleDragOver(DragEvent event) {
    if(event.getDragboard().hasFiles()){
        event.acceptTransferModes(TransferMode.ANY);
    }

}

@FXML
private void handleDropped(DragEvent event) throws IOException {
    imageView.setImage(null);
    List<File> files = event.getDragboard().getFiles();
    file = files.get(0);
    fileName.setText(file.getAbsolutePath());
    BufferedReader br;

    if(file != null){
        isFolder = checkFileExtension(file.getAbsolutePath());
        //label.setText(notify(isFolder));

        if(isFolder==true){
           br = checkDataJSFile(file.getAbsolutePath());

           if(br != null){

            boolean debugMode = checkDataJSDebugMode(br);

            if(debugMode){
                 label.setText("debugMode is true. No action performed.");
                 button.setVisible(false);
            }else{
                label.setText("Click below to generate the feedback version of this course.");
                button.setVisible(true);
                br.close();
            }
           }else{
               label.setText("The file 'data.js' was not found.");
           }

        }

    }else{
        label.setText("Drag a valid folder to process.");
    }

}

private boolean checkFileExtension(String filePath){
    boolean isValid=false;
    int i = filePath.lastIndexOf('.');
    //System.out.println(filePath);
    if(i<0){
        isValid=true;
    }
    //System.out.println("isValid value is: "+ isValid );
    return isValid;
}

private boolean checkDataJSDebugMode(BufferedReader br) throws IOException{
    boolean debugMode =false;
    String paramToFind="\"debugMode\":true";
    String dataJSFileContents;
    System.out.println("checkDataJSDebugMode()");

    dataJSFileContents=br.readLine();
    System.out.println(dataJSFileContents);
    if(dataJSFileContents.contains(paramToFind)){
        debugMode=true;
    }


    return debugMode;
}

private BufferedReader checkDataJSFile(String source){
    //boolean isPresent = false;
    BufferedReader br=null;

    File folder = new File(source+""+locationStr);

    try {
        br = new BufferedReader(new FileReader(folder));
        //isPresent = true;
    } catch (FileNotFoundException ex) {
        label.setText("Folder does not contain the data.js file!");
    }

    return br;
}

private void createJSFile(String sourceJSContents,String location) throws IOException{

    String paramToFind = "\"debugMode\":false";
    Pattern word = Pattern.compile(paramToFind);
    System.out.println("createJSFile()");

    Matcher match = word.matcher(sourceJSContents);

    int s=0,e=0;
    while(match.find()){
        s = match.start();
        e = match.end();
    }

    String changeParam = "\"debugMode\":true";
    String toWrite = sourceJSContents.substring(0, s-1)+","+changeParam+","+sourceJSContents.substring(e+1,sourceJSContents.length());
    System.out.println(toWrite);
    File dataJS = new File(location);
    BufferedWriter bw = new BufferedWriter(new FileWriter(dataJS,false));
    bw.write(toWrite);
    bw.close();
}

private File copyDir(File src) throws IOException{
    String newFileName= src.getName()+" - FEEDBACK VERSION";
    File newDir= new File(src.getParent()+"\\"+newFileName);

    if(newDir.exists()){
        newDir.delete();
    }
    newDir.mkdir();

    FileUtils.copyDirectory(src,newDir);
    return newDir;
}


@FXML
private void handleDragEntered(DragEvent event) throws FileNotFoundException {
    Image img = new Image(new FileInputStream("assets\\loader.gif"));
    imageView.setImage(img);
}

}

任何帮助将不胜感激。

0 个答案:

没有答案