问题描述
我正在使用银行gui应用程序,而我的jdialog的setvisible方法似乎有问题.用户提取有效金额后,我弹出一个简单的对话框,显示交易正在进行中".在我的dobackground方法中,我不断轮询以检查是否已收到交易.我尝试使用swingworker,但我不明白为什么它不起作用.如果我删除setvisible调用,它可以正常工作,那么为什么setvisible导致系统挂起?这是我的jbutton mouselistener内的代码:
SwingWorker< String,Integer>worker =新的SwingWorker< String,Integer>(){JDialog waitForTrans =新的JDialog((JFrame)null,true);公共字符串doInBackground()引发异常{waitForTrans.add(new JLabel(正在更新系统中的余额.请稍候..."));waitForTrans.setMinimumSize(新尺寸(300,100));waitForTrans.setDefaultCloseOperation(DISPOSE_ON_CLOSE);waitForTrans.setVisible(true);Bank.getInstance().sendTransaction(currentPin,-" + withdraw);while(!Bank.getInstance().hasCompletedTransaction){}返回null;}公共无效done(){尝试 {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();
首先,建议在Swing Event-Dispatch线程中进行所有GUI更新,即使用 SwingUtilites
类./p>
第二,您的 JDialog
是模态的,因此阻塞了调用 setVisible(true)
方法的线程(在您的情况下为Main线程,在以下情况下)Swing事件调度线程).
我并不是说下面的代码是完美的,但是它应该使您步入正轨...
最后的JDialog waitForTrans = new JDialog((JFrame)null,true);SwingWorker worker = new SwingWorker(){公共字符串doInBackground()引发异常{Thread.sleep(5000);返回null;}公共无效done(){SwingUtilities.invokeLater(new Runnable(){公共无效run(){waitForTrans.setVisible(false);waitForTrans.dispose();}});}};worker.execute();SwingUtilities.invokeLater(new Runnable(){公共无效run(){waitForTrans.add(new JLabel("Please Wait ..."));waitForTrans.setMinimumSize(new Dimension(300,100));waitForTrans.setVisible(true);}});
希望这会有所帮助.
I have banking gui application that I am currently working on and there seems to be a problem with the setvisible method for my jdialog. After the user has withdrawn a valid amount I pop up a simple dialog that says "transaction in progress". In my dobackground method i keep polling to check if the transaction has been received. I tried using swingworker and I don't understand why it's not working. If i remove the setvisible call it works fine, so why does setvisible cause the system to hang? Here is the code that is inside my 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();
First, it is recommended to do all the GUI updates in the Swing Event-Dispatch thread, i.e. using the SwingUtilites
class.
Second, your JDialog
is modal and so blocks the thread in which the setVisible(true)
method is called (in your case the Main thread, in the following case the Swing Event-Dispatch Thread).
I do not say the following code is perfect, but it should put you on the track...
final JDialog waitForTrans = new JDialog((JFrame) null, true);
SwingWorker worker = new SwingWorker() {
public String doInBackground() throws Exception {
Thread.sleep(5000);
return null;
}
public void done() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
waitForTrans.setVisible(false);
waitForTrans.dispose();
}
});
}
};
worker.execute();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
waitForTrans.add(new JLabel("Please Wait..."));
waitForTrans.setMinimumSize(new Dimension(300, 100));
waitForTrans.setVisible(true);
}
});
Hope this helps.
这篇关于java swing挂起系统中的setvisible方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!