我正在开发一个基于 java swing 的应用程序,其中有两个JFrames: A,它是主窗口,以及B,它被调用A。我需要做的是, in A, call B,从中获取用户输入B并将该输入传递给A某种方式,然后处理它我尝试过的一切都失败了,据我所知Java不支持async/await这是我最好的尝试 public void getInfoFromB() { FormB b = new FormB(); while(b.isReady() == false){ a.setVisible(false); } a.setVisible(true); ArrayList<String> b.getData(); //... }请注意,objects extendsa和isReady 都是返回用户已填写所需所有数据的天气方法,而 getData 方法返回所述数据bJFrameFormB这种尝试似乎不起作用,因为一旦代码进入 while 循环,其他所有内容都会冻结,因为 java 似乎很难处理并行操作
2 回答

暮色呼如
TA贡献1853条经验 获得超9个赞
可以通过引用将 A 传递到 B (显然所有对象都是通过引用传递的,因此不需要使用指针)。从那里,我可以操纵来自 A 和 B 的数据
A级
public void getDataFromB(ArrayList<String> data)
{
this.dataFromB = data;
}
public void startFormB()
{
this.setVisible(false);
B formB = new B(this);
}
B级
//constructor
public B(formA a)
{
this.mainForm = a;
//...
}
public void setDataToAAndExit()
{
this.mainForm.getDataFromB(this.dataToA);
//re-show main window
this.mainForm.setVisible(true);
//close this window
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
添加回答
举报
0/150
提交
取消