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

huangapple go评论78阅读模式
英文:

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?

Here's how I want the program to work: When the user clicks a button in the JFrame, a JDialog pops up. 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. If the user clicks the "Cancel" button, the data is discarded and the JDialog closes.

I thought about using JOptionPane, but I don't think it allows for data of various types. 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. I thought about creating a variable in the JDialog, but I don't know how to stop the JDialog from immediately passing an empty variable to the 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);
}

}"

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

}"

英文:

How do I pass user-inputted date from a JDialog to the parent JFrame when the user clicks a certain button in the JDialog?

Here's how I want the program to work: When the user clicks a button in the JFrame, a JDialog pops up. 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. If the user clicks the "Cancel" button, the data is discarded and the JDialog closes.

I thought about using JOptionPane, but I don't think it allows for data of various types. 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. 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.

Any help?

Code for 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.

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

答案1

得分: 2

>我考虑在JFrame中创建一个方法,并从JDialog中调用它,但我不知道如何引用JFrame。

尽管这可能是一个不错的解决方案,但引用调用方法的对象是不被鼓励的。
查看原因(第一个回答):https://stackoverflow.com/questions/15329566/how-to-find-the-object-that-called-a-method-in-java/15330843#15330843

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

在修改过的newTask(JFrame frame)方法内部,你只需使用传递的参数引用父级JFrame中的方法。

这可能看起来与引用调用方法的对象相同,但实际上并不是。在这里,你将父级JFrame作为参数传递,这不一定总是调用该方法的对象。

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

英文:

> 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.

Even though this could be a nice solution, referencing the object who called the method is very discouraged.
See why (first answer): https://stackoverflow.com/questions/15329566/how-to-find-the-object-that-called-a-method-in-java/15330843#15330843

One possible approach would be to create a method in the JFrame and adding a JFrame as a parameter for your newTask() function. Then, when invoking newTask() you pass this as an argument: newTask(this); .

Inside the modified newTask(JFrame frame) method, you just use the argument passed to reference the method in the parent JFrame.

This might seem the same as referencing the object who called the method, but it's not. Here you are passing as an argument the parent JFrame, which may not always be the object which invoked the method.

I hope I was clear enough, have a nice day!

huangapple
  • 本文由 发表于 2020年8月8日 15:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63312723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定