gpt4 book ai didi

java - SwingWorker 调用 JFrame 类 ..window 不显示任何内容

转载 作者:行者123 更新时间:2023-12-01 11:26:16 24 4
gpt4 key购买 nike

class class1{
public class1(){//here is my GUI commants}

@Override
public void actionPerformed(ActionEvent evt) //this is my action performed from a jframe window
{

worker = new SwingWorker<Void, Void>(){//ia m creating a worker
protected WaitWindow waitWindow;
@Override
protected Void doInBackground() throws Exception {
waitWindow= new WaitWindow();//i call waitWindow class to pop up my new window with the progressBar
return null;
}
@Override
protected void done(){
waitWindow.CloseWaitWindow();
}
};
try{
String option = (String)serversList.getSelectedItem();

if (evt.getSource().equals(Button1))//when client presses button1
{
if(option.equals("icsd Server"))
{//here is my connection
Registry registry = LocateRegistry.getRegistry("localhost",1080);
icsdserver = (ICSDinterface)registry.lookup("RmiCheckICSD");
worker.execute(); //i am calling execute until the server return 0 this might take a long time
if (icsdserver.RequestForEntry("icsd",0)==0)
{
worker.cancel(true); //when server tell its all ok (with 0) i call cancel(true)
AddGrade d = new AddGrade(icsdserver,"icsd");
}
}
}
}
catch (RemoteException ex) {System.out.println(ex);}
catch (NotBoundException ex) {System.out.println(ex);}
}}

等待窗口类如下

class  WaitWindow extends JFrame //my WaitWindow Class
{

private JProgressBar bar ;

public WaitWindow(){

super("Wait Until Connection Is ready");
setSize(100,200);
bar = new JProgressBar();
bar.setIndeterminate(true);
bar.setPreferredSize(new Dimension(300,330));
add(bar);
getContentPane();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}


public void CloseWaitWindow()
{
removeNotify();
}


}

我在这里做错了什么?我希望显示等待窗口,直到服务器的 RequestForEntry 方法返回 0,这可能需要一些时间。 RMI 连接也没有错误。

最佳答案

您正在通过调用 RequestForEntry 来阻止事件调度线程,该调用应位于 SwingWorkerdoInBackground 方法内,例如

public void actionPerformed(ActionEvent ev) //this is my action performed from a jframe window      
{

try {
final String option = (String) serversList.getSelectedItem();

if (evt.getSource().equals(Button1))//when client presses button1
{
final WaitWindow waitWindow = new WaitWindow();
worker = new SwingWorker<Void, Void>() {//ia m creating a worker

@Override
protected Void doInBackground() throws Exception {
if (option.equals("icsd Server")) {//here is my connection
Registry registry = LocateRegistry.getRegistry("localhost", 1080);
icsdserver = (ICSDinterface) registry.lookup("RmiCheckICSD");
worker.execute(); //i am calling execute until the server return 0 this might take a long time
if (icsdserver.RequestForEntry("icsd", 0) == 0) {
worker.cancel(true); //when server tell its all ok (with 0) i call cancel(true)
AddGrade d = new AddGrade(icsdserver, "icsd");
}
}
return null;
}

@Override
protected void done() {
waitWindow.CloseWaitWindow();
}
};
}
} catch (RemoteException ex) {
System.out.println(ex);
} catch (NotBoundException ex) {
System.out.println(ex);
}
}

Swing 是一个单线程框架,并且不是线程安全的。这意味着任何阻塞事件调度线程的事情都会阻止它处理新事件,包括绘制请求。

Swing 组件也应该只在 EDT 上下文中更新,这就是 SwingWorker 的用武之地。

参见Concurrency in SwingWorker Threads and SwingWorker了解更多详情

关于java - SwingWorker 调用 JFrame 类 ..window 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30779339/

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