Java自学-图形界面 容器

Swing 的容器 JFrame和JDialog

java的图形界面中,容器是用来存放 按钮,输入框等组件的。

窗体型容器有两个,一个是JFrame,一个是JDialog

步骤 1 : JFrame

JFrame是最常用的窗体型容器,默认情况下,在右上角有最大化最小化按钮

JFrame

package gui;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestGUI {
public static void main(String[] args) {

//普通的窗体,带最大和最小化按钮
JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);

f.add(b);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);
}
}

步骤 2 : JDialog

JDialog也是窗体型容器,右上角没有最大和最小化按钮

JDialog

package gui;

import javax.swing.JButton;
import javax.swing.JDialog;

public class TestGUI {
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);
}
}

步骤 3 : 模态JDialog

当一个对话框被设置为模态的时候,其背后的父窗体,是不能被激活的,除非该对话框被关闭

package gui;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

public class TestGUI {
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);

}
}

步骤 4 : 窗体大小不可变化

通过调用方法 setResizable(false); 做到窗体大小不可变化

package gui;

import javax.swing.JButton;
import javax.swing.JFrame;

public class TestGUI {
public static void main(String[] args) {

JFrame f = new JFrame("LoL");
f.setSize(400, 300);
f.setLocation(200, 200);
f.setLayout(null);
JButton b = new JButton("一键秒对方基地挂");
b.setBounds(50, 50, 280, 30);

f.add(b);
// 窗体大小不可变化
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);
}
}

更多内容,点击了解: Swing 的容器 JFrame和JDialog

posted on 2020-08-08 20:07  半米高峰  阅读(323)  评论(0)    收藏  举报

导航