Swing 父子窗口之间传值

何为父子

这里的父子并不是谁的窗口大谁就是父,谁的窗口小谁就是子,而是一个已存在的窗口是父窗口,由该已存在存在窗口引起新的窗口出现,则新的窗口就是子窗口。可以简单理解为谁先出现谁就是父窗口,谁后出现谁就是子窗口(不太严谨)。

父窗口向子窗口传值

前提:现存窗口A(父窗口),窗口A有一文本输入框,输入文本并键入回车后打开窗口B并将文本内容传值到窗口B(子窗口),并在窗口B的JLable标签中显示。

思路:
在窗口B中设一public属性,并在B的构造器里面初始化,这样从窗口A启动窗口B时,只需把窗口B的构造器里面传入要传入的参数即可。

A窗口:

public class A {

	private JFrame frame;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					A window = new A();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public A() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					System.out.println(textField.getText().toString());
					B window = new B(textField.getText().toString());
					window.frame.setVisible(true);
				}
			}

		});
		textField.setBounds(176, 116, 66, 21);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
	}
}

B窗口:

public class B {

	JFrame frame;
	String getContent = "";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					B window = new B("");
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public B(String getContent) {
		this.getContent = getContent;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel bLabel = new JLabel(getContent);
		bLabel.setFont(new Font("微软雅黑 Light", Font.PLAIN, 14));
		bLabel.setBounds(137, 53, 193, 35);
		frame.getContentPane().add(bLabel);
	}
}

效果图:
在这里插入图片描述
在这里插入图片描述

子窗口向父窗口传值

前提:窗口A已存在,窗口B由窗口A打开。窗口B里面有一JTextFeid,键入文本回车后在窗口A里面的JLabel标签会显示接收到的内容。

思路:在窗口A启动窗口B时,将窗口A的引用赋值于窗口B,那么在窗口B里面创建窗口A的类实例对象,并调用该实例对象的相关setXxx()getXxx()即可。

窗口A:

public class A {

	private JFrame frmA;
	private JLabel aLabel;
	
	// 自定义set方法,供于子窗口调用赋值
	public void setJLabel(JLabel aLabel){
		this.aLabel = aLabel;
	}
	
	public JLabel getJLabel(){
		return aLabel;
	}
	
	public void setFrame(JFrame frmA){
		this.frmA = frmA;
	}
	
	public Frame getFrame(){
		return frmA;
	}
	
	// 启动时将窗口A的引用赋予窗口B
	public void openSubFrame(){
		B window = new B(this);
		window.frmB.setVisible(true);
	}
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					A window = new A();
					window.frmA.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public A() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmA = new JFrame();
		frmA.setTitle("A");
		frmA.setBounds(100, 100, 450, 300);
		frmA.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmA.getContentPane().setLayout(null);
		
		aLabel = new JLabel("New label");
		aLabel.setBounds(140, 24, 154, 30);
		frmA.getContentPane().add(aLabel);
		
		JButton btnNewButton = new JButton("Click");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openSubFrame();
			}
		});
		btnNewButton.setBounds(165, 101, 93, 23);
		frmA.getContentPane().add(btnNewButton);
	}
}

窗口B:

public class B {

	JFrame frmB;
	private static A aFrame;
	
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					B window = new B(aFrame);
					window.frmB.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public B(A aFrame) {
		this.aFrame = aFrame;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmB = new JFrame();
		frmB.setTitle("B");
		frmB.setBounds(100, 100, 450, 300);
		frmB.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmB.getContentPane().setLayout(null);
		
		textField = new JTextField();
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent arg0) {
				if(arg0.getKeyCode() == KeyEvent.VK_ENTER){
					aFrame.getJLabel().setText(textField.getText().toString());
				}
			}
		});
		textField.setBounds(187, 92, 66, 21);
		frmB.getContentPane().add(textField);
		textField.setColumns(10);
	}

}

效果图:

仅有窗口A
在这里插入图片描述

窗口A(父窗口)启动窗口B(子窗口)
在这里插入图片描述

窗口B(子窗口)操纵TextField传入数据到窗口A(父窗口)
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值