对话框JDialog(模态对话框,非模态对话框)
package com.lddx.day0309;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
//对话框JDialog(模态对话框,非模态对话框)
public class TestDialog {
public static void main(String[] args) {
TestDialog d=new TestDialog();
d.init();
}
JFrame f=new JFrame();
public void init(){
JPanel p=new JPanel();
JButton b1=new JButton("模态按钮");
JButton b2=new JButton("非模态按钮");
//把内部类绑定到按钮
MyListener ml=new MyListener();
b1.addActionListener(ml);
b2.addActionListener(ml);
p.add(b1);
p.add(b2);
f.add(p);
f.setTitle("对话框");
f.setSize(600, 600);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//定义内部类实现接口ActionListener接口
public class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String str=e.getActionCommand();//获得事件源内容
System.out.println(str);
if(str.equals("模态按钮"))
{
JDialog d=new JDialog(f,"模态对话框");
d.setModal(true);//设置对话框为模态对话框
d.setSize(300, 300);
d.setTitle("What are doing!");
d.setVisible(true);//设置对话框可见
}
else
{
JDialog d=new JDialog(f,"非模态对话框");
d.setModal(false);//设置对话框为非模态对话框
d.setSize(300, 300);
d.setTitle("你弄啥嘞!");
d.setVisible(true);//设置对话框可见
}
}
}
}