如何从JFrame获取JDialog文本字段的值

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

How to get JDialog textFiels Value from JFrame

问题

以下是您提供的Java代码的翻译部分:

package com.myapp.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;

public class MyFrame extends JFrame {

private JPanel contentPane;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyFrame frame = new MyFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public MyFrame() {
		setTitle("MyFrame");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnClickMe = new JButton("Click Me");
		btnClickMe.setBounds(271, 171, 115, 29);
		contentPane.add(btnClickMe);
		
		JCheckBox chckbxOpenDialog = new JCheckBox("Open Dialog");		
		chckbxOpenDialog.setBounds(25, 171, 139, 29);
		contentPane.add(chckbxOpenDialog);
		
		chckbxOpenDialog.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(chckbxOpenDialog.isSelected() == true){
					MyDialog MD = new MyDialog();
					MD.setModal(true);
					MD.setVisible(true);
				}
			}
		});
		
		btnClickMe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
                 //Here I want to get the value of textFieldName and textFieldEmail from JDialog
                 //after Click on Confirm Button in JDialog and closing/disposing it
			}
		});
	}
}
package com.myapp.ui;

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 javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MyDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textFieldName;
	private JTextField textFieldEmail;
	private JButton btnConfirm;

	public static void main(String[] args) {
		try {
			MyDialog dialog = new MyDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public MyDialog() {
		setTitle("MyDialog");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		textFieldName = new JTextField();
		textFieldName.setBounds(108, 26, 146, 26);
		contentPanel.add(textFieldName);
		textFieldName.setColumns(10);
		
		textFieldEmail = new JTextField();
		textFieldEmail.setBounds(108, 68, 146, 26);
		contentPanel.add(textFieldEmail);
		textFieldEmail.setColumns(10);
		
		btnConfirm = new JButton("Confirm");
		btnConfirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//passing the Name and Email field value in JFrame
			}
		});
		btnConfirm.setBounds(132, 141, 115, 29);
		contentPanel.add(btnConfirm);
		
		MyFrame MF = new MyFrame();
		
	}
}

请注意,我只提供了代码的翻译部分,没有其他内容。如果您有任何问题或需要进一步的帮助,请随时提问。

英文:

Hi I am new in Java coding and trying to design a user friendly desktop App with the help of JFrame and JDialog -

My JFrame is -

package com.myapp.ui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JCheckBox;
public class MyFrame extends JFrame { 
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MyFrame frame = new MyFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public MyFrame() {
setTitle("MyFrame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnClickMe = new JButton("Click Me");
btnClickMe.setBounds(271, 171, 115, 29);
contentPane.add(btnClickMe);
JCheckBox chckbxOpenDialog = new JCheckBox("Open Dialog");		
chckbxOpenDialog.setBounds(25, 171, 139, 29);
contentPane.add(chckbxOpenDialog);
chckbxOpenDialog.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(chckbxOpenDialog.isSelected()== true){
MyDialog MD = new MyDialog();
MD.setModal(true);
MD.setVisible(true);
}
}
});
btnClickMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Here I want to get the value of textFieldName and textFieldEmail from JDialog
//after Click on Confirm Button in JDialog and closing/disposing it
}
});
}
}

My JDialog is -

package com.myapp.ui;
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 javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class MyDialog extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField textFieldName;
private JTextField textFieldEmail;
private JButton btnConfirm;
public static void main(String[] args) {
try {
MyDialog dialog = new MyDialog();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public MyDialog() {
setTitle("MyDialog");
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
textFieldName = new JTextField();
textFieldName.setBounds(108, 26, 146, 26);
contentPanel.add(textFieldName);
textFieldName.setColumns(10);
textFieldEmail = new JTextField();
textFieldEmail.setBounds(108, 68, 146, 26);
contentPanel.add(textFieldEmail);
textFieldEmail.setColumns(10);
btnConfirm = new JButton("Confirm");
btnConfirm.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//passing the Name and Email field value in JFrame
}
});
btnConfirm.setBounds(132, 141, 115, 29);
contentPanel.add(btnConfirm);
MyFrame MF = new MyFrame();
}
}

Both the JFrame and JDialog are in the same package. I am trying to get the value of

textFieldName and textFieldEmail from JDialog to JFrame

if any one can guide me the best possible way, would be relly great.

答案1

得分: 1

你可以:

  1. MyFrame添加设置器(setters)。
  2. ActionListener中创建MyDialog时,将MyFrame的引用(使用this)传递给它。
  3. MyDialogActionListener中设置MyFrame的字段。

可能有更好的方法来做这件事。内置的JOptionPane生成等待用户输入的模态对话框窗口。这些对话框比将JFrame的引用传递给JDialog要好。

另外需要注意的是,JavaFX在桌面用户界面方面已在很大程度上取代了Java Swing

英文:

You could:

  1. Add setters to MyFrame
  2. Pass MyFrame reference to MyDialog (using this) when it is created in the ActionListener
  3. In the ActionListener in MyDialog set fields in MyFrame

There are probably better ways to do this. The built in JOptionPane produces modal dialog windows that wait for user input. These would be better rather than passing references of JFrames to JDialog.

Also note that JavaFX has largely replaced Java Swing for desktop UIs

huangapple
  • 本文由 发表于 2020年9月17日 20:15:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63937863.html
匿名

发表评论

匿名网友

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

确定