一、Swing组件
1.1Swing组件的类体系结构

JFrame(窗体、框架) JButton(按钮) JLabel(标签) JTextField(文本框) JTextArea(文本域)
1.2 窗体 JFrame
//容器组件是指可以容纳其他组件的组件
public class MyFrame extends JFrame{ ..... }
导入包
javax.swing.JFrame
常用的构造方法
| JFrame() | 创建新窗体,该窗体初始为不可见 |
| JFrame(String title) | 创建新窗体,使用参数title指定标题,该窗体初始为不可见 |
常用的API
| 方 法 原 型 | 说 明 |
| void setTitle(String title) | 设置窗体标题,标题内容由参数title指定 |
| void setSize(int width, int height) | 设置窗体的大小,参数width指定宽度,参数height指定高度,单位为像素 |
| void setResizable(boolean resizable) | 设置窗体能否调整大小,由参数resizable决定 |
| void setVisible(boolean b) | 设置窗体是否为可见,由参数b决定,true为可见,false为不可见 |
| Container getContentPane() | 获得当前窗体的内容面板 |
| void setDefaultCloseOperation(int operation) | 设置窗体在关闭时默认执行的操作 |
| void dispose() | 释放当前窗体及其所有子组件所占用的资源,即卸载窗体 |
| void repaint() | 重新绘制当前窗体 |
案例
/**
* @TODO
* @Author Chushaoyang
* 2025/3/21
* 1.extends JFrame
* 2.API
* setTitle() 设置标题
* setSize() 设置宽高
* setLocationRelativeTo() 设置居中
* setDefaultCloseOperation() 设置关闭方式,关闭并退出
* setVisible(true) 设置可见
* 默认布局 Layout=java.awt.BorderLayout 边界式布局
*/
public class MyFrame extends JFrame {
public MyFrame() {
//设置标题
this.setTitle("标题");
//设置宽高
// this.setSize(320,500); //这语句跟下一行作用是一样的
this.setSize(new Dimension(320,500));
//设置出现的位置 居中显示
this.setLocationRelativeTo(null);
//设置出现 ,可以在创建窗体的时候,设置出现(一般是将显示命令放在创建窗口的时候出现)
//this.setVisible(true);
//设置关闭方式 JFrame.EXIT_ON_CLOSE关闭并退出
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
//创建窗体,设置可见
new MyFrame().setVisible(true);
}
}

1.3 Container窗体的内容面板
窗体使用外部框架和内部面板组成,外部框架是指由标题栏和四边所组成空心边框,主要用来控制窗体的大小和外观。我们实际操作的是内部面板,如窗体颜色布局等
导入包 java.awt.Container
Container类通常用于操作JFrame的内容面板
常用的API
| 方 法 原 型 | 说 明 |
| void setBackground(Color bg) | 设置容器的背景色,由参数bg指定颜色 |
| void setLayout(LayoutManager mgr) | 设置容器的布局,参数是布局管理器 |
| Component add(Component comp) | 往容器中添加一个组件 |
| Component add(Component comp, int index) | 将指定组件添加到容器中的指定位置上 |
| void remove(Component comp) | 从容器中移除指定的组件 |
| void removeAll() | 从容器中移除所有组件 |
| void repaint() | 重新绘制当前容器 |
案例
import java.awt.*;
import javax.swing.*;
/**
* @TODO
* @Author Chushaoyang
* 2025/3/27
*/
public class ContentPaneDemo extends JFrame {
public ContentPaneDemo() {
super("内容面板示例");
//获得当前窗体的内容面板
Container contentPane = this.getContentPane();
//设置内容面板的背景色为红色
contentPane.setBackground(Color.RED);
//设置窗体关闭时即退出程序
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setResizable(false); //设置窗体不可调整大小
setVisible(true);
}
public static void main(String[] args) {
ContentPaneDemo cpd = new ContentPaneDemo();
}
}

1.4 面板JPanel
提供面板组件,一般使用面板来实现布局嵌套
构造方法
| 构 造 方 法 | 说 明 |
| JPanel() | 创建一个空面板 |
| JPanel(LayoutManager layout) | 创建带有指定布局的面板 |
常用的API
| 方 法 原 型 | 说 明 |
| void setBackground(Color bg) | 设置面板的背景色,由参数bg指定颜色 |
| void setLayout(LayoutManager mgr) | 设置面板的布局,参数是布局管理器 |
| Component add(Component comp) | 往面板中添加一个组件 |
| Component add(Component comp, int index) | 将指定组件添加到面板中的指定位置上 |
| void remove(Component comp) | 从面板中移除指定的组件 |
| void removeAll() | 从面板中移除所有组件 |
| void repaint() | 重新绘制当前面板 |
JPanel(面板)的操作方式与Container(内容面板很相似)
案例
import javax.swing.*;
import java.awt.*;
/**
* @TODO
* @Author Chushaoyang
* 2025/3/21
*
// * JFrame -> Container(容器) + 设置布局方式 -> JPanel -> JButton、JTextField...组件
* JPanel画布
* 1.extends JPanel
* setBounds() 设置宽高及出现位置
* setBackground() 设置背景色
*
*JPanel 默认布局方式,layout=java.awt.FlowLayout 流式布局
*/
public class MyJPanel extends JPanel {
public MyJPanel(int weight,int height,int x,int y,int red,int green,int blue){
this.setBounds(x,y,weight,height);
this.setBackground(new Color(red,green,blue));
}
public static void main(String[] args) {
//1.创建JPanel画布
MyJPanel p1 = new MyJPanel(200, 200, 0, 0, 255, 0, 0);
MyJPanel p2 = new MyJPanel(200, 200, 200, 0, 0, 255, 0);
MyJPanel p3 = new MyJPanel(200, 200, 0, 200, 0, 0, 255);
MyJPanel p4 = new MyJPanel(200, 200, 200, 200, 0, 0, 0);
//2.创建JFrame窗体
MyInnterFrame frame = new MyInnterFrame();
//3.获取JFrame的容器
Container container = frame.getContentPane();
//4.给容器中添加组件
container.add(p1);
container.add(p2);
container.add(p3);
container.add(p4);
//5.设置可见
frame.setVisible(true);
}
}
class MyInnterFrame extends JFrame {
public MyInnterFrame() {
//设置标题
this.setTitle("标题");
//这是宽高
this.setSize(new Dimension(400,400));
//设置出现位置 居中显示
this.setLocationRelativeTo(null);
//设置关闭方式
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置不可拖拽
this.setResizable(false);
System.out.println(this);
}
}

1.5对话框JDialog
导入包
javax.swing.JDialog用于在程序中创建对话框组件.与JFrame的区别在于JDialog必须依赖于其他窗口,而JFrame可以不依赖其他窗口单独存在。
构造方法
| 构 造 方 法 | 说 明 |
| JDialog() | 创建一个没有标题并且没有指定所有者的无模式对话框 |
| JDialog(Frame owner) | 创建一个没有标题但将指定的owner作为其所有者的无模式对话框 |
| JDialog(Frame owner, boolean modal) | 创建一个没有标题但有指定所有者的对话框,根据参数modal来决定它是否模式显示 |
| JDialog(Frame owner, String title) | 创建一个具有指定标题和指定所有者的无模式对话框 |
| JDialog(Frame owner, String title, boolean modal) | 创建一个有指定标题和指定所有者的对话框,参数modal决定它是否模式显示 |
| JDialog(Dialog owner, boolean modal) | 创建一个没有标题但有指定所有者的对话框,根据参数modal来决定它是否模式显示 |
常用的API
| 方 法 原 型 | 说 明 |
| void setTitle(String title) | 设置对话框的标题,标题内容由参数title指定 |
| void setSize(int width, int height) | 设置对话框的大小,参数width指定宽度,参数height指定高度 |
| void setResizable(boolean resizable) | 设置对话框能否调整大小,由参数resizable决定 |
| void setVisible(boolean b) | 设置对话框是否为可见,由参数b决定,true为可见,false为不可见 |
| Container getContentPane() | 获得当前对话框的内容面板 |
| void dispose() | 释放当前对话框及其所有子组件所占用的资源,即卸载对话框 |
案例
显示小窗口

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @TODO
* @Author Chushaoyang
* 2025/3/27
*/
public class MyJDialog extends JFrame {
private JButton btnDisplay;
public MyJDialog() {
btnDisplay = new JButton("显示子窗口");
Container cp = this.getContentPane();
cp.add(btnDisplay, BorderLayout.SOUTH);
//匿名类的方式为按钮注册监听器
btnDisplay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//实例化子窗体,并把当前主船体的引用船体给其他的构造方法
new SubFrame(MyJDialog.this);
}
});
this.setTitle("主窗口");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 300);
this.setVisible(true);
}
public static void main(String[] args) {
new MyJDialog().setVisible(true);
}
public class SubFrame extends JDialog {
//构造方法需要将所有者的引用作为参数传过来
public SubFrame(MyJDialog owner) {
//调用父类的构造方法,相对所有者模式显示
super(owner,true);
this.setTitle("子窗口");
this.setSize(100,100);
this.setResizable(false);
this.setVisible(true);
}
}
}
1.6 滚动面板组件 JScrollPane
作用:可以给组件添加滚动条
构造方法
| 构 造 方 法 | 说 明 |
| JScrollPane() | 创建一个空的JScrollPane,需要时水平和垂直滚动条都可显示 |
| JScrollPane(Component view) | 创建一个显示指定组件内容的 JScrollPane,只要组件的内容超过视图大小就会显示水平和垂直滚动条 |
| JScrollPane(Component view,int vsbPolicy, int hsbPolicy) | 创建一个显示指定组件内容的 JScrollPane ,并指定滚动条的显示策略 |
| JScrollPane(int vsbPolicy, int hsbPolicy) |
创建一个空的JScrollPane,并指定滚动条的显示策略 |
常用的API
| 方 法 原 型 | 说 明 |
| JScrollBar getHorizontalScrollBar() | 返回当前滚动面板的水平滚动条 |
| JScrollBar getVerticalScrollBar() | 返回当前滚动面板的垂直滚动条 |
| JViewport getViewport() | 返回当前滚动面板的 JViewport |
1.7按钮JButton
javax.swing.JButton 创建按钮
构造方法
| 构 造 方 法 | 说 明 |
| JButton() | 创建一个空按钮 |
| JButton(String text) | 创建一个带文本的按钮 |
| JButton(Icon icon) | 创建一个带图标的按钮 |
| JButton(String text, Icon icon) | 创建一个带文本和图标的按钮 |
常用的API
| 方 法 原 型 | 说 明 |
| void setText(String text) | 设置按钮上的文本 |
| String getText() | 获得按钮上的文本 |
| void setBackground(Color bg) | 设置按钮的背景色 |
| Color getBackground() | 获得按钮的背景色 |
| void setEnabled(boolean b) | 设置启用(或禁用)按钮,由参数b决定 |
| void setVisible(boolean b) | 设置按钮是否为可见,由参数b决定 |
| void setToolTipText(String text) | 设置按钮的悬停提示信息 |
| void setMnemonic(int mnemonic) | 设置按钮的快捷键 |
1.8 JLabel 标签
标签既可以显示文本也可以显示图像.
1.9 文本框 JTextFiled
1.10 密码框JPasswordField
javax.swing.JPasswordField 用来提供密码框组件
1.11 文本域JTextArea
1.12 复选框JCheckBox
1.13 单选按钮JRadioButton
1.14 按钮组ButtonGroup
1.15 下拉列表JComboBox
1.16 java.awt.Font Font类用来表示字体
1.17 java.awt.Color 创建颜色对象
1.18 GUI常用控件总结
javax.swing包中的常用组件:
容器 组件
JFrame
JDialog(补充:多窗体程序 以及 窗体间 传递数据)
JPanel
JScrollPane
文本 组件
JLabel
JTextField
JPasswordField
JTextArea
表单 组件
JButton
JCheckBox
JRadioButton和ButtonGroup
JComboBox
遇到具体组件,可以在IDEA重送所相关API,这里写太多可能会看不下去,结合案例比较好.
2 布局管理器
FlowLayout 流式布局管理器,从上到下排列 JPanel默认布局
BorderLayout 边界布局管理器,将容器分为5个方位 JFrame默认布局
GridLayout 网格布局管理器,将容器分为大小相等的网格
CardLayout 卡片布局管理器
2.1 FlowLayout 流式布局管理器
左对齐,中对齐,右对齐三种方式,默认中对齐,组件大小取决于组件的内容;自左至右,排满为止换行;始终算好中间位置放置组件
案例
public class FlowLayoutDemo extends JFrame{
public FlowLayoutDemo() {
this.setTitle("这是一个流式布局案例");
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//获取容器
Container contentPane = this.getContentPane();
//设置布局方式
contentPane.setLayout(new FlowLayout());
//3.添加元素组件
contentPane.add(new JTextField("文本框1"));
contentPane.add(new JTextField("文本框2"));
}
public static void main(String[] args) {
new FlowLayoutDemo().setVisible(true);
}
}
2.2 BorderLayout 边界布局管理器
上北,下南,左西,右东,中间;(指明方位BorderLayout.NORTH,否则就默认放中间,若只有一个组件,默认充斥整个窗体)
public class BordLayoutDemo extends JFrame {
public BordLayoutDemo() {
this.setTitle("这是一个边界式布局案例");
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//获取容器
Container contentPane = this.getContentPane();
//设置边界式布局
contentPane.setLayout(new BorderLayout());
//3.添加元素
contentPane.add(new JButton("北"), BorderLayout.NORTH);
contentPane.add(new JButton("南"), BorderLayout.SOUTH);
contentPane.add(new JButton("西"), BorderLayout.WEST);
contentPane.add(new JButton("东"), BorderLayout.EAST);
contentPane.add(new JButton("中间"), BorderLayout.CENTER);
}
public static void main(String[] args) {
new BordLayoutDemo().setVisible(true);
}
}
2.3 GridLayout 网格布局管理器
实例化时,分配指定的行数及列数;GridLayout把每个组件限制到一个单元格
public class GridLayoutDemo extends JFrame {
public GridLayoutDemo(){
this.setTitle("网格式布局");
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//1. 获取Container容器
Container container = this.getContentPane();
//2.设置布局方式
container.setLayout(new GridLayout(3,3));
//3.添加元素组件
for (int i = 0; i < 9; i++) {
container.add(new JButton("按钮" + (i+1)));
}
}
public static void main(String[] args) {
new GridLayoutDemo().setVisible(true);
}
}
2.4 CardLayout 卡片布局管理器
/**
* @TODO
* @Author Chushaoyang
* 2025/3/21
* 卡片式布局 CardLayout
*1.JFrame->Container ->设置布局方式
*2.特点:重叠 + 像扑克一沓
*
*
* 事件
* 1.事件源 JButton
* 2.事件类型 点击事件
* 3.事件监听器
* 4.触发事件后
*/
public class GardLayoutDemo extends JFrame {
public GardLayoutDemo() {
this.setTitle("卡片式布局布局");
this.setSize(300, 300);
this.setLocationRelativeTo(null);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//1.获取容器
Container container = this.getContentPane();
container.setLayout(new BorderLayout()); //BorderLayout是边界式布局
//上面一个JPanel
JPanel panel1 = new JPanel();
CardLayout cardLayout = new CardLayout();
panel1.setLayout(cardLayout);//CardLayout卡片式布局
for (int i = 0; i < 5; i++) {
panel1.add(new JButton("按钮" + (i + 1)));
}
container.add(panel1, BorderLayout.CENTER);
//下面一个JPanel
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout()); //流式分布式
JButton prev1 = new JButton("上一个");
JButton prev2 = new JButton("下一个");
prev1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.previous(panel1);
// JOptionPane.showInputDialog("上一个");
}
});
prev2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.next(panel1);
// JOptionPane.showInputDialog("下一个");
}
});
panel2.add(prev1);
panel2.add(prev2);
container.add(panel2, BorderLayout.SOUTH);
}
public static void main(String[] args) {
new GardLayoutDemo().setVisible(true);
}
}

3.事件监听
步骤
//1.确定事件源
//2.确定事件类型
//3.确定事件监听器接口
//4.覆盖监听器中合适的方法
//5.对事件源注册监听器
java.awt.event包
中包含一系列与事件处理相关的类和接口.。一般来说,编写图像用户界面都必须显示地导入此包
每种事件监听器接口都是以类似于 XxxListener的形式来命名的,如:ActionListener、MouseListener、ItemListener等;
事件类型

| 事件类 | 说 明 | 事件源 |
| ActionEvent | 通常按下按钮,双击列表项或选中一个菜单项时,就会生成此事件 | JButton, JList, JMenuItem, TextField |
| AdjustmentEvent | 操纵滚动条时会生成此事件 | JScrollbar |
| ComponentEvent | 当一个组件移动、隐藏、调整大小或为可见时会生成此事件 | Component |
| ContainerEvent | 将组件添加至容器中或从中删除时会生成此事件 | Container |
| FocusEvent | 组件获得或失去焦点时会生成此事件 | Component |
| ItemEvent | 单击复选框或列表项时,或者当一个选择框或一个可选菜单项被选择或取消时生成此事件 | JCheckbox, JChoice, JList |
| KeyEvent | 接收到键盘输入时会生成此事件 | Component |
| MouseEvent | 拖动、移动、单击、按下或释放鼠标或在鼠标进入或退出一个组件时,会生成此事件 | Component |
| TextEvent | 在文本区或文本域的文本改变时会生成此事件 | JTextField, JTextArea |
| WindowEvent | 当一个窗口激活、关闭、正在关闭、恢复、最小化、打开或退出时会生成此事件 | Window |
监听器
监听器1:ActionListener 鼠标点击某个按钮时会产生该事件;
public interface ActionListener{
//其实现类必须重写actionPerformed方法,当事件发生时将调用该方法。
public void actionPerformed(ActionEvent ae);
}
监听器2:KeyListener
KeyListener专门用来处理键盘事件,其对应事件类型是KeyEvent;
该接口中包含有三个抽象方法,分别在不同的时刻被调用,原型如下:
public interface KeyListener{
//按下键盘上的某键时调用
public void keyPressed(keyEvent ke);
//释放键盘上的某键时调用
public void keyReleased(KeyEvent ke);
//输入某个字符时调用
public void keyTyped(KeyEvent ke);
}
监听器3:MouseListener
操作鼠标时会产生鼠标事件MouseEvent,而MouseListener用来处理鼠标的动作,其原型:
public interface MouseListener {
//鼠标按钮在某个组件上按下时调用
public void mousePressed(MouseEvent me);
//鼠标按钮在某个组件上释放时调用
public void mouseReleased(MouseEvent me);
//鼠标按钮在某个组件上点击(按下并释放)时调用
public void mouseClicked(MouseEvent me);
//鼠标进入到某个组件的范围之内时调用
public void mouseEntered(MouseEvent me);
//鼠标离开某个组件的范围之外时调用
public void mouseExited(MouseEvent me);
}
监听器4:MouseMotionListener
MouseMotionListener是专门处理鼠标运动事件的,比如将鼠标进行移动和拖动的时候,该接口的原型如下:
public interface MouseMotionListener{
//在某个组件上移动鼠标时调用
public void mouseMoved(MouseEvent me);
//在某个组件上拖动(按下键并移动)鼠标时调用
public void mouseDragged(MouseEvent me);
}
监听器5:ItemListener
对于像下拉列表、单选按钮、复选按钮这些有选项的组件而言,当它们的选项发生改变的时候,都会产生选项事件ItemEvent,如果需要处理这样的事件,就用到了ItemListener,其原型:
public interface ItemListener{
//当选项的状态(选择或取消)发生改变时调用
public void itemStateChanged(ItemEvent ie);
}
监听器6:WindowListener
操作窗口时会产生窗口事件WindowEvent,其对应监听器是WindowListener,原型如下:
public interface WindowListener {
//窗口被激活时调用
public void windowActivated(WindowEvent we);
//窗口被禁止时调用
public void windowDeactivated(WindowEvent we);
//窗口被关闭时调用
public void windowClosed(WindowEvent we);
//窗口正在关闭时调用
public void windowClosing(WindowEvent we);
//窗口最小化时调用
public void windowIconified(WindowEvent we);
//窗口恢复时调用
public void windowDeiconified(WindowEvent we);
//窗口打开时调用
public void windowOpened(WindowEvent we);
}
监听器7:FocusListener
某个组件得到/丢失焦点时将产生焦点事件FocusEvent,可以使用FocusListener来处理这样的事件,该接口原型:
public interface FocusListener{
//某个组件获得焦点时调用
public void focusGained(FocusEvent fe);
//某个组件失去焦点时调用
public void focusLost(FocusEvent fe);
}




2494

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



