运行方法中的代码未运行

时间:2017-03-18 08:41:21

标签: java javafx runnable

我有一个加密文字的程序 当用户按下按钮时,下面的代码激活,当type.getText()返回AES Base64时,AES Base64运行,但是当type.getText()返回Strong Text Encryptor时,强文本加密器(来自Jasypt)不会运行我之所以使用runnable是因为我希望我的进度条更新而不是冻结GUI。

请注意,我使用的是JavaFX FXML,这是控制器类,它实现了Initializable。

switch (type.getText()) {

                case "AES Base64":
                    t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                strToEncrypt = input.getText();
                                bar.setProgress(0.11);
                                strPssword = pass.getText();
                                bar.setProgress(0.22);
                                AES.setKey(strPssword);
                                bar.setProgress(0.33);
                                AES.encrypt(strToEncrypt.trim());
                                bar.setProgress(0.44);
                                output.setText(AES.getEncryptedString());
                                bar.setProgress(0.55);
                                log.setText("");
                                bar.setProgress(0.66);
                                log.appendText("String to encrypt " + strToEncrypt + "\n");
                                bar.setProgress(0.77);
                                log.appendText("Password " + strPssword + "\n");
                                bar.setProgress(0.88);
                                log.appendText("Encrypted String " + AES.getEncryptedString() + "\n");
                                bar.setProgress(1.0);
                                hasChecked = false;
                            } catch (Exception ex) {
                                bar.setProgress(0);
                                Error(ex.getMessage());
                                Logger(ex.getMessage());
                            }
                        }

                    });
                    t.start();
                    break;

                case "Strong Text Encryptor":
                    Thread t = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                StrongTextEncryptor textEncryptor = new StrongTextEncryptor();
                                bar.setProgress(0.33);
                                textEncryptor.setPassword(pass.getText());
                                bar.setProgress(0.66);
                                output.setText(textEncryptor.encrypt(input.getText()));
                                bar.setProgress(1.0);
                            } catch (Exception ex) {
                                bar.setProgress(0);
                                Error(ex.getMessage());
                                Logger(ex.getMessage());
                            }
                        }
                    });
                    break;
                 }

1 个答案:

答案 0 :(得分:2)

run()方法仅在您启动第一种情况下Thread 时调用,即case "AES Base64",但您没有启动第二种情况下为Thread,只需将t.start()添加到case "Strong Text Encryptor"

即可