一、Swing组件分为三类:顶层容器、中间容器、基本组件
1.顶层容器:
1)JFrame:用于创建一个带有标题栏的窗体
例如:
package com.test.exercise2;
import javax.swing.JFrame;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame("JFrame");
jf.setSize(200, 200);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
2)JDialog:用于创建对话框窗体
例如:(在事件处理中的例子)
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame("JFrame");
jf.setSize(200, 200);
jf.setLocationRelativeTo(null);
final JButton jb = new JButton("按钮");
jf.add(jb);
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(jb, "你按了按钮", "消息对话框", 1);
}
};
jb.addActionListener(listener);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
创建对话框窗口的主要类。可以使用此类创建自定义的对话框,或者调用 JOptionPane
中的多个类方法来创建各种标准对话框
3)JWindow:用于创建一个不带有标题栏和边框的窗体
package com.test.exercise2;
import javax.swing.JWindow;
public class Test {
public Test() {
init();
}
public void init() {
JWindow jw = new JWindow();
jw.setSize(200, 200);
jw.setLocationRelativeTo(null);
jw.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
4)JApplet(不建议用):用于创建一个applet小应用程序
2.中间容器:
1)JPanel:中间层非常重要的容器,可以无限叠加放置,支持创建一个面板对象
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame();
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
JPanel jp = new JPanel();
jp.setBackground(Color.red);
// setSize()方法只对顶层容器有用,中间容器即基本组件设置大小如下
Dimension mension = new Dimension(100, 100);
jp.setPreferredSize(mension);
jf.add(jp, BorderLayout.WEST);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
2)JScrollPane:滚动面板,创建一个带有滚动条的面板,最主要的是结合一个基本组件使用(JTextArea、JTextField)
package com.test.exercise2;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame();
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
JTextArea jta = new JTextArea();
JScrollPane jsp = new JScrollPane(jta);
Dimension mension = new Dimension(100, 100);
jsp.setPreferredSize(mension);
jf.add(jsp);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
3)JTabbedPane:选项卡面板,支持创建带有若干标签的选项卡窗格,它允许用户通过单击具有给定标题和/或图标的选项卡,在一组组件之间进行切换。
3.基本组件:功能性组件,不属于容器,在组件上不能继续添加组件
1)标签:JLabel,用于短文本字符串或图像或二者的显示区。标签不对输入事件作出反应。因此,它无法获得键盘焦点。
2)按钮:JButton(按钮)、JRadioButton(单选按钮,通常与ButtonGroup使用,就是一个组内只有一个可以选)、JCheckBox(复选按钮,可以多选)
3)列表:JList(显示对象列表并且允许用户选择一个或多个项的组件)、JComboBox(下拉列表)
4)输入框:JTextField(文本框,只有一行)、JPasswordField( 主要用来密码输入的)、JTextArea(文本区,多行文本)
5)工具条:JToolBar、JToolTip、JProgressBar
6)选择器:JFileChooser、JColorChooser
7)菜单:JMenuBar(菜单条)、JMenu(菜单)、JMenuItem(菜单项)、JCheckBoxMenuItem(复选菜单项)、JRadioButtonMenuItem(单选菜单项)、JPopupMenu
8)树表:JTree、JTable
9)提示框:JOptionPane
二、布局管理器
布局概念:布局是指组件摆放到容器中的排列方式,布局一般设置到容器对象上,每个容器需要往上添加组将时,都需要先设置好布局
1.流式布局:FlowLayout
package com.test.exercise2;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame();
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
jf.setLayout(new FlowLayout(0, 0, 0));
for (int i = 0; i < 10; i++) {
JButton jb = new JButton();
Dimension mension = new Dimension(50, 50);
jb.setPreferredSize(mension);
jf.add(jb);
}
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
默认在第一行的居中位置,横纵向间隙为5px,JPanel默认的是FlowLayout布局
2.边框布局:BorderLayout
JFrame窗体默认布局
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame();
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
JButton bt1 = new JButton(), bt2 = new JButton(), bt3 = new JButton(), bt4 = new JButton(), bt5 = new JButton();
jf.add(bt1, BorderLayout.NORTH);
jf.add(bt2, BorderLayout.SOUTH);
jf.add(bt3, BorderLayout.WEST);
jf.add(bt4, BorderLayout.EAST);
jf.add(bt5);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
3.网格布局:GridLayout
package com.test.exercise2;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame();
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
jf.setLayout(new GridLayout(3, 3));
for (int i = 0; i < 9; i++) {
JButton jb = new JButton();
jf.add(jb);
}
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
4.null布局(空布局)
容器不使用任何一个布局,添加组件则需要通过绝对定位方式添加(坐标定位)
空布局使用步骤:
1)创建容器对象
2)设置容器布局为空
3)组件对象设置位置和大小:调用setBounds()方法
4)组件添加到容器中
package com.test.exercise2;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Test {
public Test() {
init();
}
public void init() {
JFrame jf = new JFrame();
jf.setSize(500, 500);
jf.setLocationRelativeTo(null);
jf.setLayout(null);
JButton jb = new JButton();
jb.setBounds(100, 100, 50, 50);
jf.add(jb);
jf.setDefaultCloseOperation(3);
jf.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}