1 /**
2 * JDilog学习笔记
3 * @author Wfei
4 *
5 */
6 public class JDialogKnow extends JFrame
7 {
8 JDialog jDialog;
9 JButton jButton;
10 public JDialogKnow()
11 {
12 init();
13
14 this.setTitle("主窗体");
15 this.setLayout(null);
16 this.setSize(500, 500);
17 this.setLocationRelativeTo(null);
18 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
19
20 this.add(jButton);
21 }
22 public void init()
23 {
24 /*************************JDialog学习******************************/
25 //第一个参数:即该Dialog属于哪个窗体、对话框、窗口
26 //第二个参数:即该Dialog是属于模式对话框,还是属于非模式对话框
27 jDialog = new JDialog(this, true);
28 jDialog.setLayout(null);
29 jDialog.setTitle("我是Dialog");
30 jDialog.setSize(300, 200);
31 jDialog.setLocationRelativeTo(null);
32 JLabel jLabel = new JLabel("我是Dialog中的Lable");jLabel.setBounds(10, 10, 200, 30);
33 //jDialog也是类似于容器的,因此可以在其中添加组件
34 jDialog.add(jLabel);
35 //这里默认是false,只有在某事件发生时,才会触发该Dialog的呈现,本例通过Button来触发事件
36 // jDialog.setVisible(false);
37
38 jButton = new JButton("点击我 - 打开Dialog");
39 jButton.setBounds(50, 50, 200, 30);
40 jButton.addActionListener(new ActionListener()
41 {
42 @Override
43 public void actionPerformed(ActionEvent e)
44 {
45 jDialog.setVisible(true);
46 //或jDialog.show();
47 }
48 });
49 }
50 public static void main(String[] args)
51 {
52 JDialogKnow jDialogKnow = new JDialogKnow();
53 jDialogKnow.setVisible(true);
54 }
55 }