gpt4 book ai didi

java - JDialog 停止执行父 JFrame

转载 作者:行者123 更新时间:2023-12-02 00:05:00 26 4
gpt4 key购买 nike

我有一个 gif 动画图像,显示 jDialog 内的无限循环加载进度...但问题是当我加载此 jDialog 时,父帧代码停止了。?如何做到这一点..这是我的代码..

ProgressDialouge pbDialog = new ProgressDialouge(this);
pbDialog.setVisible(true);
pbDialog.toFront();
postPairs.add(new BasicNameValuePair("PATH","authenticateUser.idoc"));
postPairs.add(new BasicNameValuePair("user_email",email));
postPairs.add(new BasicNameValuePair("user_password",password));
JSONArray jArray = asyncService.sendRequest(postPairs);
if(jArray != null){
new NewJFrame().setVisible(true);

this.setVisible(false);
}

如果我更改 JDiaog 的 ModalityType.MODELESS ,它不会停止代码的执行,但也不会显示进度条..

最佳答案

很可能您遇到了线程问题,即您在 Swing 事件线程上运行长时间运行的任务,从而阻止事件线程更新 GUI。解决方案是使用后台线程,例如 SwingWorker 提供的线程。

我的猜测是,有问题的行是这一行:

JSONArray jArray = asyncService.sendRequest(postPairs);

所以,再次在后台线程中执行此操作。有关更多信息,请查看此链接:Concurrency in Swing

例如:

import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;

import javax.swing.*;

public class ShowSwingWorker {
private JPanel mainPanel = new JPanel();
private JButton myBtn = null;
private ProgressDialouge pbDialog = null;

public ShowSwingWorker() {
myBtn = new JButton(new AbstractAction("Push Me") {

@Override
public void actionPerformed(ActionEvent evt) {
JButton source = (JButton) evt.getSource();
source.setEnabled(false); // disable button
Window win = SwingUtilities.getWindowAncestor(source);
new MySwingWorker().execute(); // start background thread

if (pbDialog == null) {
pbDialog = new ProgressDialouge(win);
pbDialog.pack();
pbDialog.setLocationRelativeTo(win);
Point loc = pbDialog.getLocation();
pbDialog.setLocation(loc.x - 100, loc.y - 100);
}
pbDialog.setVisible(true);
// pbDialog.toFront();
}
});

mainPanel.add(myBtn);
}

public JComponent getMainPanel() {
return mainPanel;
}

private class MySwingWorker extends SwingWorker<Void, Void> {
@Override
protected Void doInBackground() throws Exception {
Thread.sleep(4000); // emulate a long-running task

// postPairs.add(new BasicNameValuePair("PATH",
// "authenticateUser.idoc"));
// postPairs.add(new BasicNameValuePair("user_email", email));
// postPairs.add(new BasicNameValuePair("user_password", password));
// JSONArray jArray = asyncService.sendRequest(postPairs);
// if (jArray != null) {
// new NewJFrame().setVisible(true);
//
// this.setVisible(false);
// }
return null;
}

@Override
protected void done() {
// Here you change your display.
// you were swapping JFrames, but I recommend that you instead change views.
myBtn.setEnabled(true);
pbDialog.setVisible(false);
}
}

private class ProgressDialouge extends JDialog {

public ProgressDialouge(Window win) {
super(win, "MyDialog", ModalityType.APPLICATION_MODAL);
JProgressBar pBar = new JProgressBar();
pBar.setIndeterminate(true);
add(pBar);
}

}

private static void createAndShowGUI() {
ShowSwingWorker paintEg = new ShowSwingWorker();

JFrame frame = new JFrame("ShowSwingWorker");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

关于java - JDialog 停止执行父 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14064088/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com