GUI的概念
到目前为止,我们编写的都是基于控制台的程序,不能面向用户操作。GUI(Graphical User Interface)即图形用户界面,它能够使应用程 序看上去更加友好;
Swing概述
Swing的概述
- Swing是纯Java组件,使得应用程序在不同的平台上运行时具有相同外观和相同 的行为。
- Swing中的大部分组件类位于javax.swing包中.
- Swing中的组件非常丰富,支持很多功能强大的组件.
容器组件
容器组件
Java的图形用户界面的基本组成部分是组件,组件是一个以图形化的方式显示在屏幕上并能与用户进行交互的对象.组件不能独立的显示出来,必须放在一定的容器(面板)中,才能显示出来。容器可以容纳多个组件,通过调用容器的add(Component comp)方法 向容器中添加组件。 窗口(Frame)和面板(Panel)是最常用的两个容器。
常用容器
JFrame用于在Swing程序中创建窗体;
常用的一些显示面板方法,写在代码中
import javax.swing.*;
public class Demo1 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo1 demo1 = new Demo1();
demo1.setWindow();
}
}
设置标题图标的话一般选择一张图片,然后左键选中idea的项目复制。或者吧截图放在桌面上,缩小idea的界面,直接把图片拖进idea。
这是一些方法
JPanel提供面板,它是轻量级的容器; 面板中可以添加其它组件,也可以设置布局,我们一般使用面板来实现布局嵌套;
- 框架(JFrame) 内部包含一个名叫Container(内容面板)的面板容器,如果要给 框架中添加图形控件,通常将它们加入到这个内容面板中
- 通过JFrame类中的getContentPane()方法即可获得此框架的内容面板。
- 也可以自己创建Jpanel面板对象,把JPanel作为一个组件添加到某个容器中.
import javax.swing.*;
import java.awt.*;
public class Demo2 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
JPanel jPanel = new JPanel();
jPanel.setBackground(Color.BLUE);//设置颜色
//创建按钮组件
JButton jButton1 = new JButton("测试按钮1");
JButton jButton2 = new JButton("测试按钮1");
jPanel.add(jButton1);
jPanel.add(jButton2);
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo2 demo2 = new Demo2();
demo2.setWindow();
}
}
布局管理器
swing中,提供了布局管理器类的对象可以管理,每个Jpanel都有一个布局管理器对象,当容器需要对某个组件进行定位或判 断其大小尺寸时,就会调用其对应的布局管理器,调用Jpanel的setLayout方 法改变其布局管理器对象或通过构造方法设置。
三种布局管理器:
- 流式FlowLayout
- 边界布局BorderLayout
- 网格布局GridLayout
FlowLayout流式布局:FlowLayout布局管理器是流式布局管理器,它将组件按照从左到右、从上到下的顺序来安排, 并在默认情况下使组件尽量居中放置。 几个方法写在代码中 有注释
import javax.swing.*;
import java.awt.*;
//流式布局
public class Demo3 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
//面板的布局方式默认是流式布局,默认的间隔都是5
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,10));//可以设置对齐方式,间隔的大小
jPanel.setBackground(Color.BLUE);//设置颜色
//创建按钮组件
JButton jButton1 = new JButton("测试按钮1");
JButton jButton2 = new JButton("测试按钮2");
JButton jButton3 = new JButton("测试按钮3");
JButton jButton4 = new JButton("测试按钮4");
JButton jButton5 = new JButton("测试按钮5");
JButton jButton6 = new JButton("测试按钮6");
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(jButton4);
jPanel.add(jButton5);
jPanel.add(jButton6);
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo3 demo2 = new Demo3();
demo2.setWindow();
}
}
BorderLayout边界布局:BorderLayout布局管理器只允许在容器内放置5个组件,这5个组件的位置是由BorderLayout类中的 North、South、East、West和Center5个常量来确定的,他们对应着容器中的上下左右中,用法如下:
import javax.swing.*;
import java.awt.*;
//边界布局
public class Demo4 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
//边界布局,总共有五个区域,上 下 左 右 中(必须存在)
JPanel jPanel = new JPanel(new BorderLayout());
jPanel.setBackground(Color.BLUE);//设置颜色
//创建按钮组件
JButton jButton1 = new JButton("测试按钮1");
JButton jButton2 = new JButton("测试按钮2");
JButton jButton3 = new JButton("测试按钮3");
JButton jButton4 = new JButton("测试按钮4");
JButton jButton5 = new JButton("测试按钮5");
jPanel.add(jButton1,BorderLayout.NORTH);//上
jPanel.add(jButton2,BorderLayout.SOUTH);//下
jPanel.add(jButton3,BorderLayout.EAST);//右
jPanel.add(jButton4,BorderLayout.WEST);//左
jPanel.add(jButton5,BorderLayout.CENTER);//中间,也可以默认不写,也表示中
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo4 demo2 = new Demo4();
demo2.setWindow();
}
}
FlowLayout网格布局:GridLayout布局管理器是矩形网格,在网格中放置组件,每个网格的高度和宽 度都相等,组件随着网格的大小而在水平和垂直方向上拉伸,网格的大小是由容器的 大小和创建网格的多少来确定的。其用法如下:`
import javax.swing.*;
import java.awt.*;
//网格布局
public class Demo5 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
//网格布局(表格)
//从第一行开始设置,第一行沾满后换行
JPanel jPanel = new JPanel(new GridLayout(2,3));//设置行列,如果按钮数大于行*列,那么默认添加一列
jPanel.setBackground(Color.BLUE);//设置颜色
//创建按钮组件
JButton jButton1 = new JButton("测试按钮1");
JButton jButton2 = new JButton("测试按钮2");
JButton jButton3 = new JButton("测试按钮3");
JButton jButton4 = new JButton("测试按钮4");
JButton jButton5 = new JButton("测试按钮5");
jPanel.add(jButton1);
jPanel.add(jButton2);
jPanel.add(jButton3);
jPanel.add(jButton4);
jPanel.add(jButton5);
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo5 demo2 = new Demo5();
demo2.setWindow();
}
}
常用组件
标签(JLabel)
标签文本:JLabel() 创建一个空的标签
JLabel(String text) 创建一个带文本的标签
JLabel(Icon image) 创建一个带图像的标签
代码如下:
//创建标签组件,可以显示文本,图标等
JLabel jl = new JLabel("账号");//在里面可以输入文本内容
jl.setFont(new Font("楷体",Font.BOLD,20));//BOLD加粗,可以设置文本的内容
jPanel.add(textField);//添加进面板
单行文本:
import javax.swing.*;
import java.awt.*;
//流式布局
public class Demo6 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
//面板的布局方式默认是流式布局,默认的间隔都是5
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,10));//可以设置对齐方式,间隔的大小
jPanel.setBackground(Color.WHITE);//设置颜色
//创建标签组件,可以显示文本,图标等
JLabel jl = new JLabel("账号");//在里面可以输入文本内容
jl.setFont(new Font("楷体",Font.BOLD,20));//BOLD加粗,可以设置文本的内容
//单行文本输入框,可以在里面输入内容
JTextField textField = new JTextField("请输入账号",15);//可以设置文本框里面的初始值和长度
System.out.println(textField.getText());//获取输入框里面的内容
//textField.setEditable(false);//输入框不可编辑
/*
文本框的对象添加在标签前面,那么显示也是在前面
标签的对象添加在前面,那么显示也是前面
*/
jPanel.add(jl);
jPanel.add(textField);
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo6 demo2 = new Demo6();
demo2.setWindow();
}
}
多行文本:
import javax.swing.*;
import java.awt.*;
//流式布局
public class Demo7 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
//面板的布局方式默认是流式布局,默认的间隔都是5
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,10));//可以设置对齐方式,间隔的大小
jPanel.setBackground(Color.gray);//设置颜色
//创建标签组件,可以显示文本,图标等
JLabel jl = new JLabel("账号");//在里面可以输入文本内容
jl.setFont(new Font("楷体",Font.BOLD,20));//BOLD加粗,可以设置文本的内容
//多行文本输入框,可以在里面输入内容,可以换行
JTextArea jTextArea = new JTextArea(5,20);//Area表示多行,5*20表示行列
jTextArea.setLineWrap(true);//强制换行(横向不能超边界)
//jTextArea.setWrapStyleWord(true);//英文单词换行
jTextArea.setBackground(Color.GREEN);//背景颜色设置
jTextArea.setForeground(Color.BLUE);//字体颜色设置
JScrollPane jScrollPane = new JScrollPane(jTextArea);//可以自动添加滚动条的面板
/*
文本框的对象添加在标签前面,那么显示也是在前面
标签的对象添加在前面,那么显示也是前面
*/
jPanel.add(jl);
jPanel.add(jScrollPane);
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo7 demo2 = new Demo7();
demo2.setWindow();
}
}
密码框:
import javax.swing.*;
import java.awt.*;
//流式布局
public class Demo8 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
//面板的布局方式默认是流式布局,默认的间隔都是5
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,2,10));//可以设置对齐方式,间隔的大小
jPanel.setBackground(Color.GRAY);//设置颜色
//文本框
JLabel jLabel = new JLabel("密码");
jLabel.setForeground(Color.BLUE);//字体颜色
jLabel.setFont(new Font("楷体",Font.BOLD,20));
//密码框,不显示输入的内容
JPasswordField jPasswordField = new JPasswordField(15);
JButton jButton1 = new JButton("登录");//添加按钮
jButton1.setBackground(Color.RED);//设置按钮的背景颜色
jButton1.setToolTipText("点击按钮即可登录");//设置按钮的悬停提示信息
//jButton1.setEnabled(false); 设置按钮是否可以点击
//获取密码框内容
char[] chars = jPasswordField.getPassword();
String str = new String(chars);
System.out.println("密码"+str);
jPanel.add(jLabel);//文本框添加到密码框前面
jPanel.add(jPasswordField);
jPanel.add(jButton1);
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo8 demo2 = new Demo8();
demo2.setWindow();
}
}
按钮:
JButton jButton1 = new JButton("登录");//添加按钮
jButton1.setBackground(Color.RED);//设置按钮的背景颜色
jButton1.setToolTipText("点击按钮即可登录");//设置按钮的悬停提示信息
//jButton1.setEnabled(false); 设置按钮是否可以点击
jPanel.add(jButton1);//按钮添加到面板
this.add(jPanel);//面板添加到对窗口对象中
菜单:
import javax.swing.*;
import java.awt.*;
public class Demo9 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
JPanel jpanel = new JPanel();
jpanel.setBackground(Color.GRAY);
//创建菜单栏
JMenuBar jMenuBar = new JMenuBar();
//创建菜单
JMenu jMenu1 = new JMenu("文件");
JMenu jMenu2 = new JMenu("编辑");
JMenu jMenu3 = new JMenu("帮助");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenuBar.add(jMenu3);
//添加菜单选项
JMenuItem ji1 = new JMenuItem("新建");
JMenuItem ji2 = new JMenuItem("打开");
JMenuItem ji3 = new JMenuItem("保存");
JMenuItem ji4 = new JMenuItem("清空");
JMenuItem ji5 = new JMenuItem("建议");
JMenuItem ji6 = new JMenuItem("反馈");
JMenuItem ji7 = new JMenuItem("关于我们");
jMenu1.add(ji1);
jMenu1.add(ji2);
jMenu2.add(ji3);
jMenu2.add(ji4);
jMenu3.add(ji5);
jMenu3.add(ji6);
jMenu3.add(ji7);
this.setJMenuBar(jMenuBar);//菜单栏添加到窗口而非面板
this.add(jpanel);
this.setVisible(true);//显示窗口,一般放到最后
}
public static void main(String[] args) {
Demo9 demo1 = new Demo9();
demo1.setWindow();
}
}
事件处理
对于GUI来说,如果借用了布局管理器来是界面更完善,我们编写的图形用户界面程序都仅仅只是完成了界面,而没有任何实际的功能。要实现相应的功能,必须进行事件处理
用户与GUI组件进行交互就会发生事件,如:按下一个按钮、用键盘输 入一个字符、点击鼠标等等;这就是一个事件,我们要做的就是把事件监听到,然后进行处理或者不处理。
监听器的类型就是ActionListener。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//对话框
public class Demo11 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
//面板的布局方式默认是流式布局,默认的间隔都是5
JPanel jPanel = new JPanel();//可以设置对齐方式,间隔的大小
jPanel.setBackground(Color.BLUE);//设置颜色
/*//创建标签组件,可以显示文本,图标等
JLabel jl = new JLabel("");//在里面可以输入文本内容
jl.setFont(new Font("楷体",Font.BOLD,20));//BOLD加粗,可以设置文本的内容*/
//单行文本输入框,可以在里面输入内容
JTextField textField = new JTextField(15);//可以设置文本框里面的初始值和长度
JButton jButton = new JButton("点下试试");
//textField.setEditable(false);//输入框不可编辑
/*
文本框的对象添加在标签前面,那么显示也是在前面=
标签的对象添加在前面,那么显示也是前面
*/
jPanel.add(textField);
jPanel.add(jButton);
this.add(jPanel);
this.setVisible(true);//显示窗口,一般放到最后
//监听器 创建ActionListener接口的匿名内部类
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String str = textField.getText();
if(str.length()==0){
//单对话框
//JOptionPane.showMessageDialog(null, "账号不能为空");
JOptionPane.showMessageDialog(null, "账号不能为空", "操作提示", JOptionPane.PLAIN_MESSAGE);
}else {
System.out.println(str);
}
}
});
}
public static void main(String[] args) {
Demo11 demo2 = new Demo11();
demo2.setWindow();
}
确定对话框
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Demo12 extends JFrame {
public void setWindow(){
this.setSize(600,600);//设置大小,长宽
this.setLocationRelativeTo(null);//设置起始位置,居中
this.setTitle("自制小程序");//设置标题
this.setResizable(false);//大小固定
this.setIconImage(new ImageIcon("picture1.png").getImage());
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭窗口时结束程序运行
//在窗口中添加面板
JPanel jpanel = new JPanel();
jpanel.setBackground(Color.GRAY);
//创建菜单栏
JMenuBar jMenuBar = new JMenuBar();
//创建菜单
JMenu jMenu1 = new JMenu("文件");
JMenu jMenu2 = new JMenu("编辑");
JMenu jMenu3 = new JMenu("帮助");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenuBar.add(jMenu3);
//添加菜单选项
JMenuItem ji1 = new JMenuItem("新建");
JMenuItem ji2 = new JMenuItem("打开");
JMenuItem ji3 = new JMenuItem("保存");
JMenuItem ji4 = new JMenuItem("清空");
JMenuItem ji5 = new JMenuItem("建议");
JMenuItem ji6 = new JMenuItem("反馈");
JMenuItem ji7 = new JMenuItem("退出");
jMenu1.add(ji1);
jMenu1.add(ji2);
jMenu2.add(ji3);
jMenu2.add(ji4);
jMenu3.add(ji5);
jMenu3.add(ji6);
jMenu3.add(ji7);
this.setJMenuBar(jMenuBar);//菜单栏添加到窗口而非面板
this.add(jpanel);
this.setVisible(true);//显示窗口,一般放到最后-6*
ji7.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//JOptionPane.showConfirmDialog(null, "您确定要退出吗?");
int a = JOptionPane.showConfirmDialog(null, "您确定要退出吗?");
//JOptionPane.showConfirmDialog(null, "您要退出吗", "操作框", JOptionPane.YES_NO_CANCEL_OPTION);
//默认情况下,是-0,否-1,取消-2,
if(a==0){
dispose();//释放窗口
//this.dispose(); 在匿名内部类中,this表示该内部类的对象 new ActionListener 所以不能用this
}
}
});
}
public static void main(String[] args) {
Demo12 demo1 = new Demo12();
demo1.setWindow();
}
}