gpt4 book ai didi

java - 让 "Calling method"等待数据 "Return"

转载 作者:行者123 更新时间:2023-12-01 18:24:05 25 4
gpt4 key购买 nike

我有 2 个类,一个类是 JFrame (MainUIHolder.java),另一个类是 JDialog ( EditValuationsDialog.java)。 MainUIHolder 可以在按钮单击事件上调用 EditValuationsDialog

一旦打开EditValuationsDialog,用户就可以在其字段中输入数据并按其“添加”按钮。好的,现在问题来了。用户按下“添加”按钮后,EditValuationsDialog 应将此信息通知给 MainUIHolder

下面是代码。

MainUIHolder

  Action edit = new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
JTable table = (JTable)e.getSource();
int rowNum = Integer.valueOf(e.getActionCommand());
Object valueAt = table.getModel().getValueAt(rowNum, 0);

EditValuationsDialog edit = new EditValuationsDialog(null,true);
edit.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
edit.setTitle("Edit Valuations");
edit.setClientName(portfolioViewClientName.getText());
edit.setPortfolioType(portfolioViewInvestmentTypeCombo.getSelectedItem().toString());
edit.setPortfolioId(id);
edit.setOngoingValuationsId(Integer.parseInt(String.valueOf(valueAt)));
edit.setLocationRelativeTo(table);
edit.setVisible(true);

//CATCH THE CALL FROM EditValuationsDialog HERE!!!!//

}
};

编辑评估对话框

//Action Listeners
private class AddBtnAction implements ActionListener
{

@Override
public void actionPerformed(ActionEvent e)
{
if(someCondition)
{
return String / int to MainUIHolder (See where I want to catch it in MainUIHolder)
}
else
{
do nothing
}

}
}

在我的代码中,我已指出应从何处生成对 MainUIHolder 的调用,以及我必须在 MainUIHolder 中的哪个位置捕获该调用。我怎样才能完成这个回调工作?

最佳答案

你可以...

EditValuationsDialog 添加一个 static 方法,用于显示对话框、评估结果并返回您期望的值...

public void actionPerformed(ActionEvent e)
{
int result = EditValuationsDialog.showDialog();
}

public class EditValuationsDialog ... {
//...
private int result = -1;
//...
public int getResult() {
return result;
}
//...
public static int showDialog(Component source, int rowNum, Object valueAt) {

EditValuationsDialog edit = null;
Window parent = SwingUtilities.windowFor(source);
if (parent instanceof Frame) {
edit = new EditValuationsDialog((Frame)parent,true);
} else if (parent instanceof Dialog) {
edit = new EditValuationsDialog((Dialog)parent,true);
} else {
edit = new EditValuationsDialog(null,true);
}

edit.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
edit.setTitle("Edit Valuations");
edit.setClientName(portfolioViewClientName.getText());
edit.setPortfolioType(portfolioViewInvestmentTypeCombo.getSelectedItem().toString());
edit.setPortfolioId(id);
edit.setOngoingValuationsId(Integer.parseInt(String.valueOf(valueAt)));
edit.setLocationRelativeTo(source);
edit.setVisible(true);

return edit.getResult();
}
//...
private class AddBtnAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if(someCondition)
{
result = 0;
}
else
{
result = 1;
}
EditValuationsDialog.this.dispose();
}
}
}

或者你可以...

直接评估上面示例中 getResult() 的结果...

旁注:因为我不喜欢从 JDialog 这样的顶级容器进行扩展,所以我倾向于使用 static< 创建一些面板/组件 showDialog 方法,例如登录面板的一些东西。这意味着我可以在其他地方重复使用该面板,但为我提供了在需要时能够弹出对话框的便利。我还时不时地使用 JOptionPane 来显示这些面板,但这取决于可用操作的复杂性...

关于java - 让 "Calling method"等待数据 "Return",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26842041/

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