我不知道我是否可以用一种很酷的方式解释我的方法......假设我需要来自 JDialog 的 productPrice 和 amount ,他将从用户那里获取该信息,我需要从 JFrame 调用它。
将 productPrice 和 ammount 声明为 JDialog 中的公共非静态全局变量。
public float productPrice;
public int amount;
* 这进入对话框的类全局范围内。
在 JDialog 构造函数中添加这些行以确保模态
super((java.awt.Frame) null, true);
setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
* 这在对话框的类构造函数中
假设你的 JDialog 的类名是“MyJDialog”,当调用做这样的事情时
MyJDialog question = new MyJDialog();
MyJDialog.setVisible(true);
// Application thread will stop here until MyJDialog calls dispose();
// this is an effect of modality
//
// When question calls for dispose(), it will leave the screen,
// but its global values will still be accessible.
float myTotalCostVar = question.productPrice * question.ammount;
// this is acceptable.
// You can also create public getter function inside the JDialog class,
// its safer and its a good practice.
* 这会出现在您的 JFrame 中的任何函数中,并将调用 JDialog 来获取信息。