繁体   English   中英

如何将值从JDialog框返回到父JFrame?

[英]How can I return a value from a JDialog box to the parent JFrame?

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

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

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

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

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

我通常这样做:

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

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

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

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

setVisible(false);
dispose();

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

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

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

如果您在这里查看有关对话框的Oracle教程,请说明

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

(您可以找到CustomDialog来源,看看他们如何设计您将设计自定义对话框)

我不知道我是否能够以一种很酷的方式解释我的方法...假设我需要productPrice和JDialog的数量来获取用户的信息,我需要从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来获取信息。

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

 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中获取这些值。 更多信息

这是我通常这样做的方式。 我不确定,这就是为什么我创建了这个帖子:

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

添加一个接口到您的构造函数?

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);
        }
    }
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM