48

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

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

问题是这个函数在 JDialog 框中不可见(即使 JDialog 框有对父 JFrame 的引用)。

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

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

4

6 回答 6

116

我一般是这样做的:

Dialog dlg = new Dialog(this, ...);
Value result = dlg.showDialog();

Dialog.showDialog()函数如下所示:

ReturnValue showDialog() {
    setVisible(true);
    return result;
}

由于在 JDialog 上将可见性设置为 true 是一种模式操作,因此 OK 按钮可以将实例变量 ( result) 设置为对话框的选定结果(或者null如果取消)。在 OK/Cancel 按钮方法中处理后,执行以下操作:

setVisible(false);
dispose();

将控制权返回给showDialog()函数。

于 2010-11-03T16:42:35.753 回答
26

getValue()您应该通过将自定义方法添加到您的 custom来做相反的事情JDialog

通过这种方式,您可以询问对话框的值,JFrame而不是通过调用JFrame自身的某些内容来设置它。

如果您在此处查看有关对话框的 Oracle 教程,它会说明

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

(您可以找到来源CustomDialog以了解他们认为您将如何设计自定义对话框)

于 2010-11-03T16:41:13.870 回答
4

我不知道我是否可以用一种很酷的方式解释我的方法......假设我需要来自 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 来获取信息。

于 2013-04-11T18:38:48.917 回答
0

当您将任何值传递给 JFrame 到 JDialog 时,然后在您想调用时创建 jdialog 和 jframe 的参数化构造函数。例如参数化的构造函数,如:

 public EditProduct(java.awt.Frame parent, boolean modal, int no) {
      //int no is number of product want to edit.
      //Now we can use this pid in JDialog and perform whatever you want.
}

当您想将值从 JDialog 传递到 JFrame 时,使用 set 和 get 方法创建一个 bean 类,使用 vector 的值并在 jframe 中获取这些值。 更多信息

于 2012-05-22T05:55:16.230 回答
0

这是我通常的做法。我不确定,这就是我创建该帖子的原因:

从 JDialog 返回值;dispose(), setVisible(false) - 示例

于 2013-08-20T08:49:15.727 回答
0

向构造函数添加接口?

public class UploadConfimation extends JDialog {

private final JPanel contentPanel = new JPanel();


public interface GetDialogResponse{
    void GetResponse(boolean response);
}



/**
 * Create the dialog.
 */
public UploadConfimation(String title, String message, GetDialogResponse result) {
    setBounds(100, 100, 450, 300);
    setTitle(title);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setLayout(new FlowLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    {
        JLabel lblMessage = new JLabel(message);
        contentPanel.add(lblMessage);
    }
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            JButton okButton = new JButton("YES");
            okButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    result.GetResponse(true);
                    dispose();
                }
            });
            buttonPane.add(okButton);
            getRootPane().setDefaultButton(okButton);
        }
        {
            JButton cancelButton = new JButton("NO");
            cancelButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    result.GetResponse(false);
                    dispose();
                }
            });
            buttonPane.add(cancelButton);
        }
    }
}

}

于 2016-04-13T05:44:32.470 回答