如何将值从JDialog框返回到父JFrame? [英] How can I return a value from a JDialog box to the parent JFrame?

查看:218
本文介绍了如何将值从JDialog框返回到父JFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个模态JDialog框,其上有一个自定义绘图和一个JButton。当我单击JButton时,JDialog框应该关闭并且应该返回一个值。

I have created a modal JDialog box with a custom drawing on it and a JButton. When I click the JButton, the JDialog box should close and a value should be returned.

我在父JFrame中创建了一个名为setModalPiece的函数,它接收一个值并且将它设置为本地JFrame变量。

I have created a function in the parent JFrame called setModalPiece, which receives a value and sets it to a local JFrame variable.

问题是从JDialog框中看不到此函数(即使JDialog框具有对父JFrame的引用) 。

The problem is that this function is not visible from the JDialog box (even though the JDialog box has a reference to the parent JFrame).

两个问题:
1)有没有更好的方法将值从JDialog框返回到其父JFrame?

Two questions: 1) Is there a better way to return a value from a JDialog box to its parent JFrame?

2)为什么不能使用传递给JDialog的JFrame的引用来访问我的JFrame函数setModalPiece?

2) Why can't the reference to the JFrame passed to the JDialog be used to access my JFrame function setModalPiece?

推荐答案

您应该通过向自定义 JDialog 添加自定义方法 getValue()来执行相反操作。

You should do the opposite by adding a custom method getValue() to your custom JDialog.

通过这种方式,您可以从 JFrame 中询问对话框的值,而不是通过调用某些东西来设置它在 JFrame 本身。

In this way you can ask the value of the dialog from the JFrame instead that setting it by invoking something on the JFrame itself.

如果您查看有关对话框的Oracle教程,请此处它声明

If you take a look at Oracle tutorial about dialogs here it states


如果您正在设计自定义对话框,则需要设计对话框的API,以便查询对话框中的内容用户选择了。例如,CustomDialog有一个getValidatedText方法,该方法返回用户输入的文本。

If you're designing a custom dialog, you need to design your dialog's API so that you can query the dialog about what the user chose. For example, CustomDialog has a getValidatedText method that returns the text the user entered.

(你可以找到 CustomDialog 的来源,看看他们是怎么想你的将设计您的自定义对话框)

(you can find source of CustomDialog to see how they suppose that you will design your custom dialog)

这篇关于如何将值从JDialog框返回到父JFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆