一、Swing介绍
1、Swing是一个为Java设计的GUI(图形用户界面)工具包。2、Swing由纯Java实现,在所有操作系统上都可以使用。3、Swing组件位于javax.swing包。4、在Swing GUI中,JFrame是一个顶层容器,可以存放其他组件。
二、创建窗体的常用设置

【实例】
1 import javax.swing.*;
2 public class SwingTest extends JFrame {
3 public SwingTest(){
4 //设置窗体的标题
5 this.setTitle("窗体标题");6 //设置窗体的位置与大小
7 this.setBounds(20,20,300,400);
8 //设置窗体大小是否可以改变
9 this.setResizable(false);
10 //设置关闭窗体时执行的操作
11 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
12 //设置窗体的布局方式
13 this.setLayout(null);
14 //设置窗体的位置
15 this.setLocationRelativeTo(null);
16 //设置窗体是否可见
17 this.setVisible(true);
18 }
19
20 public static void main(String[] args) {
21 new SwingTest();
22 }
23 }
【效果图】

三、窗体中常用组件【实例1】
1 import javax.swing.*;
2 public class ControlsTest extends JFrame {
3 public ControlsTest(){
4 this.setTitle("窗体常用组件测试");
5 this.setSize(300,400);
6 this.setLocationRelativeTo(null);
7 this.setLayout(null);
8 this.setResizable(false);
9 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
10 //文本标签
11 JLabel jLabel1 = new JLabel("账号:");
12 jLabel1.setBounds(20,20,50,30);
13 //文本框标签
14 JTextField jTextField1 = new JTextField();
15 jTextField1.setBounds(80,20,180,30);
16 //密码框
17 JPasswordField jPasswordField1 = new JPasswordField();
18 jPasswordField1.setBounds(80,70,180,30);
19 //下拉框
20 JComboBox jComboBox1 = new JComboBox();
21 jComboBox1.setBounds(20,120,150,30);
22 jComboBox1.addItem("1班");
23 jComboBox1.addItem("2班");
24 jComboBox1.addItem("3班");
25 jComboBox1.addItem("4班");
26 jComboBox1.addItem("5班");
27 jComboBox1.addItem("6班");
28 jComboBox1.setMaximumRowCount(3); //下拉框每次显示的最大数目
29 //单选按钮
30 JRadioButton jRadioButton1 = new JRadioButton("男");
31 jRadioButton1.setBounds(200,170,80,30);
32 //复选框
33 JCheckBox jCheckBox1 = new JCheckBox("篮球");
34 jCheckBox1.setBounds(20,220,80,30);
35 //将组件加入到窗体中
36 this.add(jLabel1);
37 this.add(jTextField1);
38 this.add(jPasswordField1);39 this.add(jComboBox1);
40 this.add(jRadioButton1);
41 this.add(jCheckBox1);
42 this.setVisible(true);
43 }
44
45 public static void main(String[] args) {
46 new ControlsTest();
47 }
48 }
【效果图】

【实例2】
1 import javax.swing.*;
2 public class JTableTest extends JFrame {
3 public JTableTest(){
4 this.setTitle("表格组件");
5 this.setSize(400,400);
6 this.setLocationRelativeTo(null);
7 //定义表格表头
8 String[] heads = {"编号","姓名","性别","年龄"};
9 //定义表格内容10 Object[][] data = {
11 {"1001","张三","男",20},
12 {"1002","李四","女",18},
13 {"1003","王五","女",19},
14 {"1004","赵二","男",21},
15 };
16 //创建JTable对象
17 JTable jTable1 = new JTable(data,heads);
18 int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
19 int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
20 //创建滚动条对象
21 JScrollPane jsp = new JScrollPane(jTable1,v,h);
22 this.add(jsp);
23 this.setVisible(true);
24 }
25
26 public static void main(String[] args) {
27 new JTableTest();
28 }
29 }
【结果图】

【实例2】
1 import javax.swing.*;
2 import java.awt.*;
3
4 public class MenuTest extends JFrame {
5 public MenuTest(){
6 this.setTitle("菜单组件");
7 this.setSize(400,400);
8 this.setLayout(new FlowLayout(FlowLayout.LEFT));
9 this.setLocationRelativeTo(null);
10 //创建菜单工具栏
11 JMenuBar jMenuBar = new JMenuBar();
12 //创建一级菜单
13 JMenu jMenu1 = new JMenu("文件(File)");
14 JMenu jMenu2 = new JMenu("编辑(Edit)");
15 JMenu jMenu3 = new JMenu("资源(Resources)");
16 //创建二级菜单
17 JMenuItem jMenuItem1_1 = new JMenuItem("新建(Ctrl+N)");
18 JMenuItem jMenuItem1_2 = new JMenuItem("打开(Ctrl+O)");
19 JMenuItem jMenuItem1_3 = new JMenuItem("保存(Ctrl+S)");
20 JMenuItem jMenuItem2_1 = new JMenuItem("复制(Ctrl+C)");
21 JMenuItem jMenuItem2_2 = new JMenuItem("粘贴(Ctrl+V)");
22 JMenuItem jMenuItem3_1 = new JMenuItem("快速定义");
23 //将二级菜单加到对应的一级菜单中
24 jMenu1.add(jMenuItem1_1);
25 jMenu1.add(jMenuItem1_2);
26 jMenu1.add(jMenuItem1_3);
27 jMenu2.add(jMenuItem2_1);
28 jMenu2.add(jMenuItem2_2);
29 jMenu3.add(jMenuItem3_1);
30 //将一级菜单加到菜单工具栏中
31 jMenuBar.add(jMenu1);
32 jMenuBar.add(jMenu2);
33 jMenuBar.add(jMenu3);
34 //将菜单工具栏加入到窗体中
35 this.add(jMenuBar);
36 this.setVisible(true);
37 }
3839 public static void main(String[] args) {
40 new MenuTest();
41 }
42 }
【结果图】

四、Swing常用布局方式
4.1 流式布局:FlowLayout

4.2 网格布局:GridLayout

4.3 边框布局:BorderLayout
BorderLayout(边框布局管理器)是 Window、JFrame 和 JDialog 的默认布局管理器。边框布局管理器将窗口分为 5 个区域:North、South、East、West 和 Center。
35万+

被折叠的 条评论
为什么被折叠?



