基础定义
GUI:图形用户界面(Graphical User Interface,又称图形用户接口)。
AWT:抽象窗口工具包(Abstract Window Toolkit)。
JFC:JAVA基础类别(Java Foundation Classes),是一个图形框架(Graphical Framework)。
Swing GUI包含:组件和容器。
组件(控件)
组件继承于JComponent类。
JComponent继承于AWT的类Component及其子类Container。
- 常见的组件
- 标签JLabel
- 按键JButton
- 输入框JTextField
- 复选框JCheckBox
- 列表JList
容器
重量级容器(顶层容器top-level container),不继承于JComponent。包括JFrame,JApplet,JWindow,JDialog。顶层容器JFrame可独立存在,可被移动,也可被最大化和最小化,有标题栏、边框,可添加菜单栏。
轻量级容器,(中间层容器),继承于JComponent,包括JPanel,JScrollPane等。中间层容器JPanel不能独立存在,必须包含在另一个容器中。
Swing简单程序
package com.company;
import javax.swing.*;
public class SwingTest extends JFrame {
public void MainWindowTest() {
// 创建一个顶层容器
JFrame jfrm = new JFrame("A Simple Swing Application");
// 给容器一个大小
jfrm.setSize(400, 150);
// 设置关闭容器停止程序
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建一个文本标签
JLabel jlab = new JLabel(" Swing defines the modern Java GUI.");
// 把标签添加到容器中
jfrm.getContentPane().add(jlab);
// 设置容器可见
jfrm.setVisible(true);
}
}
package com.company;
public class Main {
public static void main(String[] args) {
// write your code here
new SwingTest().MainWindowTest();
}
}
布局管理
布局管理器
FlowLayout:流式布局管理器,是从左到右,中间放置,一行放不下就换到另外一行。
BorderLayout:这种布局管理器分为东、南、西、北、中心五个方位。
GridLayout:网格式布局
GridBagLayout:网格式布局,可以放置不同大小的组件
BoxLayout:把组件水平或者竖直排在一起
SpringLayout:按照一定的约束条件来组织组件
各种布局管理器效果一览
此学习笔记参考原文链接:https://blog.csdn.net/cb_east/article/details/79949415