我有我目前正在处理的银行 gui 应用程序,我的 jdialog 的 setvisible 方法似乎有问题。在用户提取有效金额后,我会弹出一个简单的对话框,上面写着“正在进行交易”。在我的 dobackground 方法中,我不断轮询以检查是否已收到交易。我尝试使用 swingworker,但我不明白为什么它不起作用。如果我删除 setvisible 调用它工作正常,那么为什么 setvisible 会导致系统挂起?这是我的 jbutton mouselistener 中的代码:
SwingWorker<String,Integer> worker = new SwingWorker<String,Integer>(){
JDialog waitForTrans = new JDialog((JFrame)null,true);
public String doInBackground() throws Exception {
waitForTrans.add(new JLabel("Updating balance in system. Please Wait..."));
waitForTrans.setMinimumSize(new Dimension(300,100));
waitForTrans.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
waitForTrans.setVisible(true);
Bank.getInstance().sendTransaction(currentPin,"-"+withdraw);
while(!Bank.getInstance().hasCompletedTransaction){
}
return null;
}
public void done(){
try {
this.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
waitForTrans.setVisible(false);
newField.setText(String.valueOf(Bank.getInstance().getAccountList().get(currentPin).getBalance()));
}
};
worker.execute();