简单JDialog对话模式
JDialog(对话框)
(1)对话框主要摆放各种控件(按钮、文本框和列表框等等)
(2)JDialog构造方法
a:JDialog(),创建一个标题栏文字为空的非模式对话框
b:JDialog(Frame owner, String title, boolean model),创建一个模式或非模式的对话框,owner为该对话框的父窗口,
model为模式标志,true表示模式对话框,false则为非模式对话框
(3)关闭方式
a:DO_NOTHING_ON _CLOSE
b:HIDE_ON_CLOSE
c:DISPOSE_ON_CLOSE
(4)常用方法
a:void setTitle(String title)
b:void setModel(boolean true)
c:void show()
package com.gxa.edu;import javax.swing.JFrame;import javax.swing.JDialog;import javax.swing.JButton;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.Toolkit;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;public class JDialogDemo extends JFrame implements ActionListener {private JButton b1;private JDialog dialog;public JDialogDemo() {init();}public void init() {this.setTitle("JDialog的应用");this.setSize(500, 400);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLocation(300, 100);this.setResizable(false);this.getContentPane().setLayout(new FlowLayout());Toolkit tkit = Toolkit.getDefaultToolkit();Dimension screenSize = tkit.getScreenSize();Dimension frameSize = this.getSize();setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height)/2);b1 = new JButton("点击查看JDialog");this.getContentPane().add(b1);b1.addActionListener(this);this.setVisible(true);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubObject o = e.getSource();if (o == b1) {dialog = new JDialog(this, "我是一个JDialog", true);dialog.setDefaultCloseOperation(DISPOSE_ON_CLOSE);dialog.setSize(300, 200);dialog.setVisible(true);}}public static void main(String[] args) {new JDialogDemo();}}