【问题标题】:CLICK JAVAFXML BUTTON SCENEBUILDER单击 JAVAFXML 按钮场景构建器
【发布时间】:2022-01-19 08:42:23
【问题描述】:
@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 fxml 按钮单击事件时遇到问题。每当我单击一次按钮时,什么都没有发生。我必须第二次点击它才能工作?我可能做错了什么?我在上面分享了我的代码。我正在使用场景构建器,除了这个错误之外,我已经准备好了所有东西。除此问题外,一切正常。代码从计算机目录加载文本文件,并将数据保存到加载的文件中。

【问题讨论】:

    标签: button click fxml


    【解决方案1】:
    @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());
        }
    }
    

    【讨论】:

    • 请在您的答案中添加一些解释,以便其他人可以从中学习
    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2017-08-08
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 2015-09-07
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    相关资源
    最近更新 更多