swing的容器有两类一个是JFrame,一个是JDialog。
1.JFrame和JDialog的区别
JFrame是最常用的窗体型容器,默认情况下,在右上角有最大化最小化按钮
JDialog也是窗体型容器,右上角没有最大和最小化按钮
代码如下:
package gui3;
import javax.swing.JButton;
import javax.swing.JFrame;
public class test1 {
public static void main(String[] args) {
JFrame f = new JFrame("123");
f.setSize(400,300);
f.setLocation(200,200);
f.setLayout(null);
JButton b = new JButton("456");
b.setBounds(50, 50, 280, 30);
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
package gui3;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class test2 {
public static void main(String[] args) {
JDialog f = new JDialog();
f.setTitle("123");
f.setSize(400,300);
f.setLocation(200,200);
f.setLayout(null);
JButton b = new JButton("456");
b.setBounds(50, 50, 280, 30);
f.add(b);
f.setVisible(true);
}
}
运行结果:
根据代码配合图片可以清晰的看出JFrame右上角是有最大化和最小化的,而JDialog没有。
2.模态JDialog
当一个对话框被设置为模态的时候,其背后的父窗体,是不能被激活的,除非该对话框被关闭。
代码如下:
package gui3;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class test3 {
public static void main(String[] args) {
JFrame f = new JFrame("外部窗体");
f.setSize(800,600);
f.setLocation(100,100);
// 根据外部窗体实例化JDialog
JDialog d = new JDialog(f);
// 设置为模态
d.setModal(true);
d.setTitle("模态的对话框");
d.setSize(400, 300);
d.setLocation(200, 200);
d.setLayout(null);
JButton b = new JButton("456");
b.setBounds(50,50,280,30);
d.add(b);
f.setVisible(true);
d.setVisible(true);
}
}
结果:
可以看出不关闭456静态对话框,点击后面那个就没有反应。这里还要注意一点:这里需要手动关闭进程,因为JDialog没有setDefaultCloseOperation操作。
3.窗体大小不可变化
简单来说就是通过调用方法 setResizable(false); 做到窗体大小不可变化。
代码如下:
package gui3;
import javax.swing.JButton;
import javax.swing.JFrame;
public class test4 {
public static void main(String[] args) {
JFrame f = new JFrame("123");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("666");
b.setBounds(50, 50, 280, 30);
f.add(b);
//窗口大小不可变化
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
结果:
然后你这时候拉窗口发现怎么都拉不了,这就是窗体大小不可变。
4.练习
题目:首先设计一个JFrame,上面有一个按钮,文字是 “打开一个模态窗口”。
点击该按钮后,随即打开一个模态窗口。
在这个模态窗口中有一个按钮,文本是 “锁定大小”, 点击后,这个模态窗口的大小就被锁定住,不能改变。 再次点击,就回复能够改变大小
代码如下:
package gui3;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class test5 {
public static void main(String[] args) {
JFrame f = new JFrame("123");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("打开一个模态窗口");
b.setBounds(50, 50, 280, 30);
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JDialog d = new JDialog(f);
d.setTitle("这是一个模态窗口");
d.setModal(true);
d.setSize(200, 150);
d.setLocationRelativeTo(f);
JButton b =new JButton("锁定大小");
d.add(b);
b.addActionListener(new ActionListener() {
boolean resizable = false;
@Override
public void actionPerformed(ActionEvent e) {
d.setResizable(resizable);
resizable = !resizable;
b.setText(!resizable ? "锁定大小" : "解锁大小");
}
});
d.setVisible(true);
}
});
f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
结果:
点击运行出现如下画面:
然后点击按钮:
这时候小的窗口是能放大缩小,然后点击锁定大小
此时窗口被锁死,出现解锁大小的字样。
基本思路:就是执行两次按钮监听。