简体   繁体   English

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

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

How do I pass user-inputted date from a JDialog to the parent JFrame when the user clicks a certain button in the JDialog?当用户单击 JDialog 中的某个按钮时,如何将用户输入的日期从 JDialog 传递给父 JFrame?

Here's how I want the program to work: When the user clicks a button in the JFrame, a JDialog pops up.下面是我希望程序如何工作:当用户单击 JFrame 中的按钮时,会弹出一个 JDialog。 The user then enters some data of various types (string and integer).然后用户输入一些不同类型的数据(字符串和整数)。 If the user clicks an "Add Task" button, the data is passed back to the original JFrame, which will display the data, and the JDialog closes.如果用户单击“添加任务”按钮,则数据将传递回原来的 JFrame,它将显示数据,并且 JDialog 关闭。 If the user clicks the "Cancel" button, the data is discarded and the JDialog closes.如果用户单击“取消”按钮,数据将被丢弃并且 JDialog 关闭。

I thought about using JOptionPane, but I don't think it allows for data of various types.我考虑过使用 JOptionPane,但我认为它不允许各种类型的数据。 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.我想过在 JFrame 中创建一个方法并从 JDialog 调用它,但我不知道如何引用 JFrame。 I thought about creating a variable in the JDialog, but I don't know to stop the JDialog from immediately passing an empty variable to the JFrame.我想过在 JDialog 中创建一个变量,但我不知道要阻止 JDialog 立即将一个空变量传递给 JFrame。

Any help?有什么帮助吗?

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

}

The JFrame is launched by another class, so it doesn't have a main method. JFrame是由另一个class发射的,所以没有main方法。

Code for JDialog: 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.我想过在 JFrame 中创建一个方法并从 JDialog 调用它,但我不知道如何引用 JFrame。

Even though this could be a nice solution, referencing the object who called the method is very discouraged.尽管这可能是一个不错的解决方案,但非常不鼓励引用调用该方法的 object。 See why (first answer): How to find the object that called a method in Java查看原因(第一个答案): 如何找到调用 Java 中的方法的 object

One possible approach would be to create a method in the JFrame and adding a JFrame as a parameter for your newTask() function.一种可能的方法是在 JFrame 中创建一个方法,并添加一个 JFrame 作为newTask() function 的参数。 Then, when invoking newTask() you pass this as an argument: newTask(this);然后,在调用newTask()时,您将this作为参数传递: newTask(this); . .

Inside the modified newTask(JFrame frame) method, you just use the argument passed to reference the method in the parent JFrame.在修改后的newTask(JFrame frame)方法中,您只需使用传递的参数来引用父 JFrame 中的方法。

This might seem the same as referencing the object who called the method, but it's not.这似乎与引用调用该方法的 object 相同,但事实并非如此。 Here you are passing as an argument the parent JFrame, which may not always be the object which invoked the method.在这里,您将父 JFrame 作为参数传递,它可能并不总是调用该方法的 object。

I hope I was clear enough, have a nice day!我希望我足够清楚,祝你有美好的一天!

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

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