首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >单击JAVAFXML按钮SCENEBUILDER

单击JAVAFXML按钮SCENEBUILDER
EN

Stack Overflow用户
提问于 2021-12-16 08:29:51
回答 1查看 11关注 0票数 0
代码语言:javascript
复制
@FXML
void handleButtonAction(ActionEvent event) {
    buttonOpenFile.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
            int result = fileChooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                openFiletxtField.setText(selectedFile.getAbsolutePath());
                System.out.println("Selected file: " + selectedFile.getAbsolutePath());
            }

        }
    });

    buttonSaveFile.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

            File file = new File("Details.txt");
            try {
                FileOutputStream fOut = new FileOutputStream(file, true);
                OutputStreamWriter osw = new OutputStreamWriter(fOut);
                osw.write("whatever you need to write");
                JOptionPane.showMessageDialog(null, "Saved Successfully..");
                osw.flush();
                osw.close();
            } catch (IOException iOException) {
                System.out.println("" + iOException.getMessage());
            }

        }
    });

}

我在使用javafx按钮单击事件时遇到问题。每当我点击按钮一次,什么都不会发生。我必须第二次点击它才能起作用?我做错什么了?我在上面分享了我的代码。我正在使用场景生成器,除了这个错误之外,我已经准备好了所有的东西。除了这个问题外,一切都很顺利。代码从计算机目录加载文本文件并将数据保存到加载的文件中。

EN

回答 1

Stack Overflow用户

发布于 2021-12-16 08:59:41

代码语言:javascript
复制
@FXML
void handleOpenFileButtonAction(ActionEvent event) {

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
    int result = fileChooser.showOpenDialog(null);
    if (result == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        openFiletxtField.setText(selectedFile.getAbsolutePath());
    }

}

@FXML
void handleSaveFileButtonAction(ActionEvent event) {
    File file = new File("Details.txt");
    try {
        FileOutputStream fOut = new FileOutputStream(file, true);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.write(firstNametxtField.getText() + "," + lastNametxtField1.getText() + "," + enrolledCheckBox.isSelected() + "," + addresstxtField.getText() + "," + statesCombobox.getSelectionModel().getSelectedItem().toString() + "\n");
        JOptionPane.showMessageDialog(null, "Saved Successfully..");
        osw.flush();
        osw.close();
    } catch (IOException iOException) {
        System.out.println("" + iOException.getMessage());
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70375948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档