Java JDialog停止父JFrame的执行
我有一个gif动画图像,它显示了jDialog中的无限循环加载过程……但问题是,当我加载这个jDialog时,父帧代码是暂停的。?怎么做这是我的代码Java JDialog停止父JFrame的执行,java,multithreading,swing,jdialog,animated-gif,Java,Multithreading,Swing,Jdialog,Animated Gif,我有一个gif动画图像,它显示了jDialog中的无限循环加载过程……但问题是,当我加载这个jDialog时,父帧代码是暂停的。?怎么做这是我的代码 ProgressDialouge pbDialog = new ProgressDialouge(this); pbDialog.setVisible(true); pbDialog.toFront(); postPairs.add(new BasicNameValuePair("PATH","authenticateUser.idoc")); p
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);
同样,在后台线程中执行此操作。有关更多信息,请查看此链接:
例如:
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.awt.Point;
导入java.awt.Window;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
公共类ShowSwingWorker{
private JPanel mainPanel=new JPanel();
私有JButton myBtn=null;
private ProgressDialouge pbDialog=null;
公开展示SwingWorker(){
myBtn=newjbutton(新抽象动作(“推我”){
@凌驾
已执行的公共无效操作(操作事件evt){
JButton source=(JButton)evt.getSource();
source.setEnabled(false);//禁用按钮
Window win=SwingUtilities.getWindow祖先(源);
新建MySwingWorker().execute();//启动后台线程
如果(pbDialog==null){
pbDialog=新进度拨号(win);
pbDialog.pack();
pbDialog.setLocationRelativeTo(win);
Point loc=pbDialog.getLocation();
pbDialog.setLocation(位置x-100,位置y-100);
}
pbDialog.setVisible(true);
//pbDialog.toFront();
}
});
主面板。添加(myBtn);
}
公共JComponent getMainPanel(){
返回主面板;
}
私有类MyWingWorker扩展SwingWorker{
@凌驾
受保护的Void doInBackground()引发异常{
Thread.sleep(4000);//模拟长时间运行的任务
//添加(新的BasicNameValuePair(“路径”),
//“authenticateUser.idoc”);
//postPairs.add(新的BasicNameValuePair(“用户电子邮件”,email));
//添加(新的BasicNameValuePair(“用户密码”,password));
//JSONArray jArray=asyncService.sendRequest(postPairs);
//if(jArray!=null){
//新建NewJFrame().setVisible(true);
//
//此.setVisible(false);
// }
返回null;
}
@凌驾
受保护的void done(){
//在这里您可以更改显示。
//您正在交换JFrames,但我建议您改为更改视图。
myBtn.setEnabled(真);
pbDialog.setVisible(false);
}
}
私有类ProgressDialog扩展JDialog{
公共进程拨号(窗口赢){
super(win,“MyDialog”,ModalityType.APPLICATION\u MODAL);
JProgressBar pBar=新的JProgressBar();
pBar.SetUndeterminate(真);
添加(pBar);
}
}
私有静态void createAndShowGUI(){
ShowSwingWorker paintEg=新的ShowSwingWorker();
JFrame=新JFrame(“ShowSwingWorker”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(paintEg.getMainPanel());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
createAndShowGUI();
}
});
}
}
在Swing事件线程上运行长时间运行的任务时,很可能会出现线程问题,从而阻止事件线程更新GUI。解决方案是使用后台线程,如SwingWorker提供的线程
我的猜测是,令人不快的是:
JSONArray jArray = asyncService.sendRequest(postPairs);
同样,在后台线程中执行此操作。有关更多信息,请查看此链接:
例如:
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.awt.Point;
导入java.awt.Window;
导入java.awt.event.ActionEvent;
导入javax.swing.*;
公共类ShowSwingWorker{
private JPanel mainPanel=new JPanel();
私有JButton myBtn=null;
private ProgressDialouge pbDialog=null;
公开展示SwingWorker(){
myBtn=newjbutton(新抽象动作(“推我”){
@凌驾
已执行的公共无效操作(操作事件evt){
JButton source=(JButton)evt.getSource();
source.setEnabled(false);//禁用按钮
Window win=SwingUtilities.getWindow祖先(源);
新建MySwingWorker().execute();//启动后台线程
如果(pbDialog==null){
pbDialog=新进度拨号(win);
pbDialog.pack();
pbDialog.setLocationRelativeTo(win);
Point loc=pbDialog.getLocation();
pbDialog.setLocation(位置x-100,位置y-100);
}
pbDialog.setVisible(true);
//pbDialog.toFront();
}
});
主面板。添加(myBtn);
}
公共JComponent getMainPanel(){
返回主面板;
}
私有类MyWingWorker扩展SwingWorker{
@凌驾
受保护的Void doInBackground()引发异常{
Thread.sleep(4000);//模拟长时间运行的任务
//添加(新的BasicNameValuePair(“路径”),
//“authenticateUser.idoc”);
//postPairs.add(新的BasicNameValuePair(“用户电子邮件”,email));
//添加(新的BasicNameValuePair(“用户密码”,password));
//JSONArray jArray=asyncService.sendRequest(postPairs);
//if(jArray!=null){
//新建NewJFrame().setVisible(true);
//
//这个.setVisibl