目录
一、GUI的概念
GUI(Graphical User Interface)即图形用户,是指采用图形方式显示的用户界面,与早期计算机使用的命令行界面相比,图形界面对于用户来说在视觉上更易于接受
二、Swing概述
swing是一个为Java设计的GUI工具包javax.swing,该包中包含了图形用户界面的各种组件支持。
一个Java的图形界面,有各种不同类型的"元素"组成,这些"元素"被称为组件(Component)。
swing中的组件可以分为两大类:容器(如窗口,对话框,面板)和功能组件(如按钮,输入框,菜单等)。它们的区别主要是概念上的。因为每个容器也都是组件。
容器是一种可以包含组件的特殊组件。
三、容器组件
功能组件不能独立地显示出来,必须将组件放在一定的容器(container)中才可以显示出来。
容器中可以添加多个组件,通过调用容器的add(Component comp)方法向容器中添加组件。
窗口(JFrame)和面板(JPanel)是最常用的两个容器。
常用容器
JFrame类用来创建窗体;
我们的类可以直接继承该类,实现窗体制作。
设置窗体的常用方法
void setSize(int width, int height) //设置宽和高
void setVisible(boolean b) //让窗口显示,放在设置的最后一行void setTitle(String title) //设置标题void setResizable(boolean resizable) //设置窗口是否可以调整大小void setLocation(int x,int y) //设置窗口位置void setLocationRelativeTo(null); //设置窗口水平垂直居中void setDefaultCloseOperation(int operation) //关闭窗口时退出程序void dispose() //释放窗口所占用的所有资源
代码示例:
import javax.swing.*;
import java.awt.*;
public class LoginFrame extends JFrame {
public LoginFrame() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300,300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
LoginFrame loginFrame = new LoginFrame();//创建了一个窗口对象
}
}
实现结果:
Jpanel类
Jpanel面板,它是轻量级的容器;
面板中可以添加其他组件,也可以设置布局,我们一般使用面板来实现布局嵌套;
自己创建Jpanel面板对象,把Jpanel作为一个组件添加到窗口或某个面板中。
常用方法:
void setBackground(Color bg)设置面板的背景色,由参数bg指定颜色
void setLayout(LayoutManager mgr)设置面板的布局,参数是布局管理器
Component add(Component comp)往面板中添加一个组件
示例代码:
import javax.swing.*;
import java.awt.*;
public class DemoFrame extends JFrame {
public DemoFrame() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300, 300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
//创建一个面板
JPanel jPanel = new JPanel();
jPanel.setBackground(new Color(68, 110, 224));
JButton jButton = new JButton("按鈕");//创建一个按钮组件
jPanel.add(jButton);
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
DemoFrame demoFrame = new DemoFrame();
}
}
运行结果:
四、布局管理器
swing中,提供了布局管理器类的对象可以管理:
每个Jpanel都有一个布局管理器对象,当容器需要对某个组件进行定位或判断其大小尺寸时,就会调用其对应的布局管理器,调用Jpanel的setLayout方法改变其布局管理器对象或通过构造方法设置。
Java中的图形界面在布局管理上采用容器和布局管理相分离的方案,也就是说容器只是把组件放进来,但不管他怎样设置。至于如何放置需要用到布局管理器(Container)。Java中有几种常用的布局管理器,分别是:FlowLayout,BoardLayout,GriLayout。
1.FlowLayout
FlowLayout布局管理器就是流式布局管理器,它将组件按照从左到右,从上到下的顺序来安排,并在默认下使组件尽量居中放置。
this.setLayout(new FlowLayout);
this.add(new Button("确定"));
this.add(new Button"取消');
FlowLayout布局管理器对组件逐行定位,行内从左到右,一行排满后换行。
不改变组件的大小,按组件原有尺寸显示组件,可设置不同的组件间距,行距以及对齐方式。
构造方法
new FlowLayout(FlowLayout.RIGHT,20,40);右对齐,组件之间水平间距20个像素,垂直间距40个像素。
new FlowLayout(FlowLayout.LEFT);左对齐,水平和垂直间距为缺省值(5)。
new FlowLayout();使用缺省的居中对齐方式,水平和垂直间距为缺省值(5)。
示例代码:
import javax.swing.*;
import java.awt.*;
public class DemoFrame1 extends JFrame {
public DemoFrame1() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300,300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
/*
面板组件可以设置布局管理方式:布局管理器
FlowLayout:流式布局:也是默认的布局方式
把组件放到一起,从左到右排放,一行占满后,重新开启一行
面板默认流式布局时水平居中的
*/
//创建面板
// JPanel jPanel = new JPanel();//默认使用的流式布局,等价于下一行
JPanel jPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,20,40));//向左对齐
JButton jButton1 = new JButton("确定");//按钮是此面板中用的组件
JButton jButton2 = new JButton("取消");
jPanel.add(jButton1);
jPanel.add(jButton2);
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
DemoFrame1 demoFrame = new DemoFrame1();
}
}
运行结果:
2.BorderLayout
BorderLayout布局管理器只允许在容器内放置5个组件,这5个组件的位置是由BorderLayout类中的North,South,East,West和Center5个常量来确定的,它们对应着容器中的上下左右中,用法如下:
this.add(new Button("按钮"),BorderLayout.NORTH);
this.add(new Button("按钮"),BorderLayout.CENTER);
组件在BorderLayout中的大小都是可以改变的,一般情况下可以让中间区域大一些,而且可以只用其中几个区域。
BorderLayout将整个容器的布局划分成东(EAST),西(WEST),南(SOUTH),北(NORTH),中(CENTER)五个区域,组件只能被添加到指定的区域。
如不指定组建的加入部位,则默认加入到CENTER区。
每个区域只能加入一个组件,如加入多个,则先前加入的会被覆盖。
BorderLayout型布局容器尺寸缩放原则:
北、南两个区域在水平方向缩放。
东、西两个区域在垂直方向缩放。
中部可在两个方向上缩放。
代码示例:
import javax.swing.*;
import java.awt.*;
public class DemoFrame2 extends JFrame {
public DemoFrame2() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300,300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
/*
边界布局:总共有五个区域,每个区域可以放置一个组件,并且占满整个区域,中间区域是必须的,其他几个区域可以根据需要使用
*/
//创建面板
JPanel jPanel = new JPanel(new BorderLayout());
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.WEST);
jPanel.add(jButton4,BorderLayout.EAST);
jPanel.add(jButton5,BorderLayout.CENTER);
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
DemoFrame2 demoFrame = new DemoFrame2();
}
}
运行结果:
3.GridLayout
GridLayout布局管理器是矩形网格,在网格中放置组件,每个网格的高度和宽度都相等,组件随着网格的大小而在水平和垂直方向上拉伸,网格的大小是有容器的大小和创建网格的多少来确定的。其用法如下:
this.setLayout(new GridLayout(2,3));//创建一个2行3列的网格
this.add(new Button("按钮"));
当组件数目大于网格数时,GridLayout保持行数不变而自动增加列数。
GridLayout型布局管理器将空间划分成规则的矩形网格,每个单元格区域大小相等。组件被添加到每个单元格中,先从左到右填满一行后换行,再从上到下。
在GridLayout构造方法中指定分割的行数和列数。
示例代码:
import javax.swing.*;
import java.awt.*;
public class DemoFrame3 extends JFrame {
public DemoFrame3() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300, 300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
/*
网格布局:类似一个表格,可以设置行数和列数
每个网格中只能放一个组件,占满整个区域,从第一行开始拜访,第一行摆满后,再开启第二行
*/
//创建一个面板
JPanel jpanel = new JPanel(new GridLayout(2, 2));
JButton jButton1 = new JButton("按钮1");
JButton jButton2 = new JButton("按钮2");
JButton jButton3 = new JButton("按钮3");
JButton jButton4 = new JButton("按钮4");
jpanel.add(jButton1);
jpanel.add(jButton2);
jpanel.add(jButton3);
jpanel.add(jButton4);
this.add(jpanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
DemoFrame3 demoFrame3 = new DemoFrame3();//创建了一个窗口对象
}
}
运行结果:
五、常用组件
标签(JLabel)
标签是容纳文本和图标的控件,通常用在界面中标识别的控件。
构造函数:
JLabel() 创建一个空的标签
JLabel(String text) 创建一个带文本的标签
方法:
void setText(String text) 设置标签上的文本
String getText() 获得标签上的文本
setFont(new Font("宋体",Font.BOLD,18)); 设置字体
单行文本(JText)
JTextField的构造函数:
JTextField(int columns)
方法:
void setText(String text) //设置文本框中的文字
String getText() //获取文本框中的文字
用标签组件(JLabel)和单行文本组件(JTextField)完成的示例代码:
public class ComponentFrame extends JFrame {
public ComponentFrame() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300, 300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
//创建一个面板
JPanel jPanel = new JPanel();
JLabel jLabel = new JLabel("账号");
jLabel.setFont(new Font("楷体",Font.BOLD,20));//设置字体
jLabel.setForeground(new Color(45, 83, 238));//设置字体颜色
//单行文本框组件 columns时设置的列数
JTextField jTextField = new JTextField(15);
jTextField.getText();//获得文本框输入的内容
jPanel.add(jLabel);
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
ComponentFrame componentFrame = new ComponentFrame();
}
}
运行结果:
多行文本框(JTextArea)
构造函数:
JTextArea(int rows,int columns) //创建一个指定行数和列数的空文本域
方法:
void setText(String text) //创建文本域中的文字
String getText() //创建文本域中的文字
void setFont(Font font) //设置文本域中文本的文字
void setLineWrap(boolean wrap) //是否自动换行,默认false
如果需要文本区自动出现滚条,可将文本区对象放入滚动窗格(JScrollPane)中:
JScrollPane scrollPane = new JScrollPane(txtArea);
add(scrollPane);
用多行文本框组件(JTextArea)完成的示例代码:
import javax.swing.*;
import java.awt.*;
public class ComponentFrame1 extends JFrame {
public ComponentFrame1() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300, 300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
//创建一个面板
JPanel jPanel = new JPanel();
//多行文本组成(文本域)
JTextArea jTextArea = new JTextArea(5, 20);
jTextArea.setLineWrap(true); //设置强制换行
//带滚动条的面板 把多行文本框组建起来
JScrollPane jScrollPane = new JScrollPane(jTextArea);
jPanel.add(jScrollPane);
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
ComponentFrame1 componentFrame1 = new ComponentFrame1();
}
}
运行结果:
可以看到实现了多行文本框还加入了滚动条。
密码框:JPasswordField
构造函数:
JPasswordField(String text)
JPasswordField(String text,int columns)
方法:
char[] getPassword()
用密码框组件(JPasswordField)完成的示例代码:
import javax.swing.*;
import java.awt.*;
public class ComponentFrame2 extends JFrame{
public ComponentFrame2() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300,300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
//创建一个面板
JPanel jPanel = new JPanel();
JLabel passwordLabel = new JLabel("密码");
JPasswordField jPasswordField = new JPasswordField(15);
char[] password = jPasswordField.getPassword();//获取输入的密码
JButton jButton = new JButton("登录");
jButton.setEnabled(false);//禁用按钮
jButton.setToolTipText("点击登录");//设置鼠标放在按钮上时的显示选项
jPanel.add(passwordLabel);
jPanel.add(jPasswordField);
jPanel.add(jButton);
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
ComponentFrame2 componentFrame2 = new ComponentFrame2();//创建了一个窗口对象
}
}
运行结果:
按钮(JButton)
构造方法:
JButton(String text) //创建一个带文本的标签
方法:
void setBackground(Color bg) //设置按钮的背景色
void setEnabled(boolean b) //设置启用(或禁用)按钮,由参数b决定
void setToolTipText(String text) //设置按钮的悬停提示信息
菜单栏组件:
构造方法:JMenuBar();
方法:add(menu); 向菜单栏添加菜单
菜单组件:
构造方法:JMenu(“菜单名称");
方法:add(menuItem); 向菜单添加菜单选项
菜单项组件:
构造方法:JMenuItem(“菜单项名称");
将菜单栏添加到窗口
setJMenuBar(menuBar);
用菜单栏(JMenuBar),菜单(JMenu),菜单项(JMenuItem)组件完成的示例代码:
import javax.swing.*;
import java.awt.*;
public class ComponentFrame3 extends JFrame {
public ComponentFrame3() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300,300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
//菜单栏 菜单 菜单项
JMenuBar jMenuBar = new JMenuBar();//创建菜单栏
//创建菜单
JMenu jMenu1 = new JMenu("文件");
JMenu jMenu2 = new JMenu("编辑");
JMenu jMenu3 = new JMenu("帮助");
//创建菜单项
JMenuItem jMenuItem1 = new JMenuItem("新建");
JMenuItem jMenuItem2 = new JMenuItem("保存");
JMenuItem jMenuItem3 = new JMenuItem("剪切");
JMenuItem jMenuItem4 = new JMenuItem("复制");
JMenuItem jMenuItem5 = new JMenuItem("关于我们");
//把菜单项加入到菜单
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu2.add(jMenuItem3);
jMenu2.add(jMenuItem4);
jMenu3.add(jMenuItem5);
//把菜单添加到菜单栏
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenuBar.add(jMenu3);
this.setJMenuBar(jMenuBar);//把菜单栏添加到窗口
//创建一个面板
JPanel jPanel = new JPanel();
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
public static void main(String[] args) {
ComponentFrame3 componentFrame3 = new ComponentFrame3();//创建了一个窗口对象
}
}
运行结果:
六、事件处理
对于采用了图形用户界面的程序来说,事件控制是非常重要的;到目前为止,我们编写的图形用户界面程序都仅仅只是完成了界面,而没有任何实际的功能,要实现相应的功能,必须进行事件处理;
用户与GUI组件进行交互就会发生事件,如:按下一个按钮、用键盘输入一个字符、点击鼠标等等;
当前我们要关注的并不是“事件是如何产生的” ,而是讨论当发生事件后,我们应当“如何处理” 。
Java中,事件处理的基本思路如下:
一个事件源产生一个事件并把它送到监听器那里,监听器一直等待,直到它受到一个事件,一旦事件被接受,监听器将处理这些事件;
示例:
由于我们想要处理按钮的点击事件,因此,按钮便是事件源;
监听器类型是ActionListerner。
添加时间监听器(此处为匿名类,为了简化语法)
按钮对象.addActionListener(new ActionListener() { // 事件处理
@Override
public void actionPerformed(ActionEvent e) {
执行操作
}
});
示例代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DemoActionListener extends JFrame {
public DemoActionListener() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300, 300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
//创建一个面板
JPanel jPanel = new JPanel();
//添加多行文本框组件
JTextField jTextField = new JTextField(15);
JButton jButton = new JButton("登录");//创建一个按钮组件
jPanel.add(jTextField);
jPanel.add(jButton);
//把组件添加事件处理程序
//jButton.addActionListener(new A());
//new 接口 创建匿名内部类 为了简化语法
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("按钮被点击了");
System.out.println(jTextField.getText());
}
});
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
//内部类 用于实现接口ActionListener
// class A implements ActionListener{
//
// @Override
// public void actionPerformed(ActionEvent e) {
//
// }
// }
public static void main(String[] args) {
DemoActionListener demoActionListener = new DemoActionListener();
}
}
运行结果:
对按钮组件添加事件处理程序(鼠标处理事件,共有五种和对文本框)的示例代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DemoActionListener1 extends JFrame {
public DemoActionListener1() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300, 300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
/*
边界布局:总共有五个区域,每个区域可以放置一个组件,并且占满整个区域,中间区域是必须的,其他几个区域可以根据需要使用
*/
//创建面板
JPanel jPanel = new JPanel();
JTextField jTextField = new JTextField(15);
JButton jButton = new JButton("登录");
jButton.setEnabled(false);
jPanel.add(jTextField);
jPanel.add(jButton);
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
//为组件添加事件处理程序
//鼠标处理事件 共有五种
jButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
System.out.println("鼠标点击触发 -- 一次按下抬起");
}
@Override
public void mousePressed(MouseEvent e) {
System.out.println("鼠标按下");
}
@Override
public void mouseReleased(MouseEvent e) {
System.out.println("鼠标释放 按键抬起");
}
@Override
public void mouseEntered(MouseEvent e) {
System.out.println("鼠标移入到标签上");
}
@Override
public void mouseExited(MouseEvent e) {
System.out.println("鼠标移除标签");
}
});
//为文本框添加事件监听
jTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
System.out.println("按键摁下了" + e.getKeyChar() + e.getKeyCode());//监听键盘动作
}
});
}
public static void main(String[] args) {
DemoActionListener1 demoActionListener1 = new DemoActionListener1();
}
}
运行结果:
七、对话框
JOptionPane():创建标准对话框
showMessageDialog():显示消息对话框
主要有五种消息类型,类型不同,图标不同:
ERROR_MESSAGE 错误消息提示
INFORMATION_MESSAGE 信息提示
WARNING_MESSAGE 警告提示
QUESTION_MESSAGE 问题提示
PLAIN_MESSAGE 简洁提示
showConfirmDialog():确认对话框
主要有四种消息类型,类型不同,图标不同:
DEFAULT_OPTION 默认选项
YES_NO_OPTION 是/否选项
YES_NO_CANCEL_OPTION 是/否/取消选项
OK_CANCEL_OPTION 确定/取消
示例代码:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DemoActionListener2 extends JFrame {
public DemoActionListener2() throws HeadlessException {
this.setSize(300, 300);//设置窗口大小
this.setLocationRelativeTo(null);//设置窗口水平垂直居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口时退出程序
this.setTitle("欢迎登陆");//设置标题
this.setLocation(300, 300);//设置窗口位置
this.setResizable(false);//设置窗口为不可调整大小
//创建一个面板
JPanel jPanel = new JPanel();
//添加多行文本框组件
JTextField jTextField = new JTextField(15);
JButton jButton = new JButton("登录");//创建一个按钮组件
jPanel.add(jTextField);
jPanel.add(jButton);
//把组件添加事件处理程序
//jButton.addActionListener(new A());
//new 接口 创建匿名内部类 为了简化语法
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//获取文本框输入的内容
String account = jTextField.getText();
if(account.length()==0) {
//消息提示框
//JOptionPane.showMessageDialog(null,"请输入账号!");
//JOptionPane.showMessageDialog(null,"请输入账号!","操作提示",JOptionPane.WARNING_MESSAGE);
int res = JOptionPane.showConfirmDialog(null, "您确定要退出吗?", "操作提示", JOptionPane.OK_CANCEL_OPTION);
System.out.println(res);// 点击确定返回0 , 点击取消返回2
if (res == 0) {
//执行退出操作
}
return;
}
if(account.length()<6||account.length()>10){
JOptionPane.showMessageDialog(null,"请输入6~10位的账号");
return;
}
}
});
//把面板添加到窗口上
this.add(jPanel);
this.setVisible(true);//让窗口显示,放在设置的最后一行
}
//内部类 用于实现接口ActionListener
// class A implements ActionListener{
//
// @Override
// public void actionPerformed(ActionEvent e) {
//
// }
// }
public static void main(String[] args) {
DemoActionListener2 demoActionListener2 = new DemoActionListener2();
}
}
运行结果:
八、内部类
1.什么是内部类
把类定义在另一个类的内部,该类就被称为内部类。
如果在类 Outer 的内部再定义一个类 Inner,此时类 Inner 就称为内部类 (或称为嵌套类),而类 Outer 则称为外部类(或称为宿主类)
2.内部类定义
内部类——就是位于外部类成员位置的类
特点:可以使用外部类中所有的成员变量和成员方法(包括private的)
class Outer {
private int age = 20;
class Inner {
public void show() {
System.out.println(age);
}
}
}
3.匿名内部类
内部类仍然是一个独立的类,在编译之后内部类会被编译成独立 的.class文件,但是前面冠以外部类的类名和$符号。
内部类不能用普通的方式访问。内部类是外部类的一个成员,因此内部类可以自由地访问外部类的成员变量,无论是否为 private 的。
匿名内部类是一种特殊的局部内部类,它是通过匿名类实现接口。
new 接口名称/抽象类名称() {
重写抽象方法;
}
在实际开发中,我们常常遇到这样的情况:一个接口/抽象类的方法的某个实现方式在程序中只会执行一次,但为了使用它,我们需要创建它的实现类。此时可以 使用匿名内部类的方式,可以无需创建新的类,减少代码冗余。
public interface IA {
abstract void fun1();
}
new IA() {
@Override
public void fun1() {
System.out.println("匿名类实现fun1");
}
};
4.内部类的意义
1.封装性
将只有在某个类中使用的功能定义在内部中,并可以使用不同访问权限修饰控制。
2.实现多继承
Java是不支持多继承的,多继承有一个致命的缺陷,容易出现钻石继承结构,例如下图:
用内部类实现多继承的代码示例:
public class Father{//定义一个父类
public String powerFul ="市长”;
}
public class Mother{//定义了另一个父类
public String wealthy="一百万”;
}
public class Son {//子类son
class Extends_Father extends Father{
}
class Extends_Mother extends Mother{
}
public void sayHello()
{
String father = new Extends_Father().powerFul;
String mother = new Extends_Mother().wealthy;
System.out.printin("myfather is:" + father + "my mother has:" + mother);
}
}
通过这节,我们可以制作一个简易的登录界面:
import javax.swing.*;
import java.awt.*;
public class LoginFrame2 extends JFrame {
public LoginFrame2() throws HeadlessException {
this.setTitle("用户登录");
this.setSize(350,400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(true);
//创建总面板(网格式GridLayout)
JPanel contentPanel = new JPanel(new GridLayout(4,1,5,10));
//登录面板
JPanel welcomePanel = new JPanel();
JLabel welcomeLabel = new JLabel("登录");
welcomePanel.add(welcomeLabel);//添加标签组件到登陆面板
//账号面板
JPanel accountJpanel = new JPanel(new FlowLayout(FlowLayout.CENTER,40,20));//控制了标签和文本框的间距,也控制了和上下其他面板间距
JLabel accountJlabel = new JLabel("账号");
JTextField accountJtextfield = new JTextField(15);
accountJpanel.add(accountJlabel);
accountJpanel.add(accountJtextfield);
//密码面板
JPanel passwordJpanel = new JPanel(new FlowLayout(FlowLayout.CENTER,40,20));
JLabel passwordJlabel = new JLabel("密码");
JTextField passJtextfield = new JTextField(15);
accountJpanel.add(passwordJlabel);
accountJpanel.add(passJtextfield);
//确定或取消面板
JPanel Button = new JPanel(new FlowLayout(FlowLayout.CENTER,40,20));
JButton jButton1 = new JButton("确定");
JButton jButton2 = new JButton("取消");
Button.add(jButton1);
Button.add(jButton2);
//把各面板添加到总面板
contentPanel.add(welcomePanel);
contentPanel.add(accountJpanel);
contentPanel.add(passwordJpanel);
contentPanel.add(Button);
this.add(contentPanel);
this.setVisible(true);
}
public static void main(String[] args) {
LoginFrame2 loginFrame2 = new LoginFrame2();
}
}
运行结果: