package sayHello;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends JFrame {
	JButton bt1 =new JButton("说你好");
	
	JLabel lb =new JLabel();
	public Main() {
		setLayout(new FlowLayout());
		add(bt1);
		
		add(lb);
		this.setSize(300,200);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		bt1.addActionListener(new BtListener());
	
	}
	
	class BtListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			Child dlg =new Child(Main.this);
			dlg.setSize(200,100);
			dlg.setTitle("模态对话框");
			dlg.setLocationRelativeTo(null);//定位在显示器正中间
			dlg.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			dlg.setModal(true);//设置模态对话框
			dlg.lb.setText("你好");//向对话框传值
			dlg.setVisible(true);
		}
	}
	
	
	
	
	public static void main(String[] args) {		
		new Main();
	}

}

package sayHello;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Child  extends JDialog {
	
	Main main =null;//表示探出此对话框的主窗口,由主窗口创建对话框时传递过来
	JLabel lb =new JLabel();
	
	public Child(Main main) {
		this.main=main;//主窗口传来后,即可对主窗口及其组件进行操作
		setLayout(new FlowLayout());		
		add(lb);		
	}
	
}