容器就是用来装东西的,图形界面中的容器就是用来放其他组件的。前面的文章中已经使用到了容器组件JFrame,而放在该容器组件上的其他组件有按钮组件等等。
窗体型容器有两个,一个是JFrame,一个是JDialog,通常使用的是JFrame,JDialog主要用于模态框。这里只展示JDialog的代码,前者的代码之前的文章已经多次使用。
public class TestJDialog {
public static void main(String[] args) {
//普通的窗体,带最大和最小化按钮,而对话框却不带
JDialog d = new JDialog();
d.setTitle("LOL");
d.setSize(400, 300);
d.setLocation(200, 200);
d.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
d.add(b);
d.setVisible(true);
}
}
下面是模态框的用法,不知道什么是模态框?A窗口中弹出B窗口,此时你只能聚焦在B窗口,而不能聚焦其他窗口,例如web页面中的alert。
public class TestModal {
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("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);
d.add(b);
f.setVisible(true);
d.setVisible(true);
}
}
其他常用API:
void setResizable(boolean resizable);//窗口大小不可改变
Q.E.D.