当用户在 JDialog 中单击按钮时,如何将输入的数据从 JDialog 传递到父 JFrame?

How to pass inputted data from JDialog to parent JFrame when user clicks button in JDialog?

当用户单击 JDialog 中的某个按钮时,如何将用户输入的日期从 JDialog 传递到父 JFrame?

下面是我希望程序的工作方式:当用户单击 JFrame 中的按钮时,会弹出一个 JDialog。然后用户输入一些不同类型的数据(字符串和整数)。如果用户单击“添加任务”按钮,数据将传回原始 JFrame,后者将显示数据,然后 JDialog 关闭。如果用户单击“取消”按钮,则丢弃数据并关闭 JDialog。

我考虑过使用 JOptionPane,但我认为它不允许各种类型的数据。我考虑过在 JFrame 中创建一个方法并从 JDialog 中调用它,但我不知道如何引用 JFrame。我想在 JDialog 中创建一个变量,但我不知道如何阻止 JDialog 立即将一个空变量传递给 JFrame。

有什么帮助吗?

JFrame 代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Dialog.ModalityType;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JDialog;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainInterface extends JFrame {

    private JPanel contentPane;

    public MainInterface() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 400, 800);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JButton addTask = new JButton("Add");
        addTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                NewTask newTask = new NewTask();
                newTask.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                // Set window title
                newTask.setTitle("Add Task");
                newTask.setVisible(true);
            }
        });
        addTask.setBounds(0, 728, 97, 25);
        contentPane.add(addTask);
        
        JButton modifyTask = new JButton("Modify");
        modifyTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                
            }
        });
        modifyTask.setBounds(95, 728, 97, 25);
        contentPane.add(modifyTask);
        
        JButton deleteTask = new JButton("Delete");
        deleteTask.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            }
        });
        deleteTask.setBounds(190, 728, 97, 25);
        contentPane.add(deleteTask);
        
        JButton settingMenu = new JButton("Settings");
        settingMenu.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Setting settings = new Setting();
                settings.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                settings.setVisible(true);
                settings.setTitle("Settings");
            }
        });
        settingMenu.setBounds(285, 728, 97, 25);
        contentPane.add(settingMenu);
    }

}

JFrame 由另一个 class 启动,因此它没有 main 方法。

JDialog 代码:

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.omg.CORBA.PUBLIC_MEMBER;

import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.JSpinner;
import java.awt.event.ActionListener;
import java.util.jar.Attributes.Name;
import java.awt.event.ActionEvent;

public class NewTask extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField taskName;

    public NewTask() {
        setBounds(100, 100, 450, 600);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);
        {
            JLabel lblNewLabel = new JLabel("Name:");
            lblNewLabel.setBounds(24, 13, 38, 16);
            contentPanel.add(lblNewLabel);
        }
        {
            taskName = new JTextField();
            taskName.setBounds(79, 10, 304, 22);
            contentPanel.add(taskName);
            taskName.setColumns(10);
        }
        
        JLabel lblNewLabel_1 = new JLabel("Time Required:");
        lblNewLabel_1.setBounds(24, 58, 97, 16);
        contentPanel.add(lblNewLabel_1);
        
        JSpinner hourSpinner = new JSpinner();
        hourSpinner.setBounds(125, 55, 44, 22);
        contentPanel.add(hourSpinner);
        
        JLabel lblNewLabel_2 = new JLabel("hours");
        lblNewLabel_2.setBounds(175, 58, 44, 16);
        contentPanel.add(lblNewLabel_2);
        
        // Set maximum value for minutes to 59
        int min = 0;
        int max = 59;
        int step = 1;
        int i = 1;
        SpinnerModel value = new SpinnerNumberModel(i, min, max, step);
        JSpinner minuteSpinner = new JSpinner(value);
        minuteSpinner.setBounds(225, 55, 44, 22);
        contentPanel.add(minuteSpinner);
        
        JLabel lblNewLabel_3 = new JLabel("minutes");
        lblNewLabel_3.setBounds(281, 58, 56, 16);
        contentPanel.add(lblNewLabel_3);
        
        JLabel lblNewLabel_4 = new JLabel("Deadline:");
        lblNewLabel_4.setBounds(24, 108, 56, 16);
        contentPanel.add(lblNewLabel_4);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton addButton = new JButton("Add Task");
                addButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        dispose();
                    }
                });
                addButton.setActionCommand("OK");
                buttonPane.add(addButton);
                getRootPane().setDefaultButton(addButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        // Close dialog window
                        dispose();
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }
    
}

I thought about creating a method in the JFrame and calling it from the JDialog, but I don't know how to reference the JFrame.

尽管这可能是一个不错的解决方案,但非常不鼓励引用调用该方法的对象。 查看原因(第一个答案):How to find the object that called a method in Java

一种可能的方法是在 JFrame 中创建一个方法并添加一个 JFrame 作为 newTask() 函数的参数。然后,在调用 newTask() 时将 this 作为参数传递:newTask(this); .

在修改后的 newTask(JFrame frame) 方法中,您只需使用传递的参数来引用父 JFrame 中的方法。

这似乎与引用调用该方法的对象相同,但事实并非如此。此处您将父 JFrame 作为参数传递,它可能并不总是调用该方法的对象。

希望我说得够清楚,祝你有美好的一天!