吴志勇的博客 吴志勇的博客
  • h5

    • HTML5&CSS3
  • scss

    • css预处理语言
  • JavaScript

    • JavaScript教程
    • Ajax
    • ES6教程
    • NodeJS
    • Typescript
  • 框架

    • Jquery
    • VUE
    • React
  • Swing专题
  • java基础
  • javaweb
  • 框架
  • 数据库
  • netty
  • 设计模式
  • 微服务及架构
  • 云原生
  • maven
  • 单元测试
工具
我的
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

吴志勇

......
  • h5

    • HTML5&CSS3
  • scss

    • css预处理语言
  • JavaScript

    • JavaScript教程
    • Ajax
    • ES6教程
    • NodeJS
    • Typescript
  • 框架

    • Jquery
    • VUE
    • React
  • Swing专题
  • java基础
  • javaweb
  • 框架
  • 数据库
  • netty
  • 设计模式
  • 微服务及架构
  • 云原生
  • maven
  • 单元测试
工具
我的
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Swing专题

    • swing图形化界面简介

    • JavaSwing布局管理器

    • JavaSwing基本组件

    • JavaSwing面板组件

    • JavaSwing其他组件

      • JFrame(窗口)
      • JDialog、JOptionPane(对话框)
      • JFileChooser(文件选择器)
      • JColorChooser(颜色选择器)
      • JMenuBar(菜单栏)
      • JToolBar(工具栏)
      • JPopupMenu(弹出菜单)
      • JTable(表格)
      • JTree(树)
      • JInternalFrame(内部窗口)
    • JavaSwing相关特性

    • 扩展:JavaAWTSwing其他相关

  • java基础

  • javaweb

  • 框架

  • Maven
  • 单元测试
  • 动态代理
  • 数据库

  • netty

  • 设计模式

  • 微服务及架构

  • 云原生

  • Velocity模板引擎
  • 后端
  • Swing专题
  • JavaSwing其他组件
wuzhiyong
2024-09-18

JToolBar(工具栏)

# 1. 概述

官方JavaDocsApi: javax.swing.JToolBar (opens new window)

JToolBar,工具栏。

JToolBar 提供了一个用来显示常用控件的容器组件。

对于大多数的外观,用户可以将工具栏拖到其父容器四“边”中的一边,并支持在单独的窗口中浮动显示。为了正确执行拖动,建议将 JToolBar 实例添加到容器四“边”中的一边(其中容器的布局管理器为 BorderLayout),并且不在其他四“边”中添加任何子级。

JToolBar 常用构造方法:

/**
 * 参数说明:
 *     name: 
 *         工具栏名称,悬浮显示时为悬浮窗口的标题。
 *
 *     orientation: 
 *         工具栏的方向,值为 SwingConstants.HORIZONTAL 或 SwingConstants.VERTICAL,
 *         默认为 HORIZONTAL。
 */
JToolBar()

JToolBar(String name)

JToolBar(int orientation)

JToolBar(String name, int orientation)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

JToolBar 常用方法:

// 添加 工具组件 到 工具栏
Component add(Component comp)

// 添加 分隔符组件 到 工具栏
void addSeparator()
void addSeparator(Dimension size)

// 获取工具栏中指定位置的组件(包括分隔符)
Component getComponentAtIndex(int index)

// 设置工具栏是否可拖动
void setFloatable(boolean b)

// 设置工具栏方向,值为 wingConstants.HORIZONTAL 或 SwingConstants.VERTICAL
void setOrientation(int o)

// 设置工具栏边缘和其内部工具组件之间的边距(内边距)
void setMargin(Insets m)

// 是否需要绘制边框
void setBorderPainted(boolean b)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 2. 代码实例

本实例需要用到 3 张小图片作为按钮的图标,分别命名为: previous.png、pause.png、next.png

package com.xiets.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Main {

    public static void main(String[] args) {
        JFrame jf = new JFrame("测试窗口");
        jf.setSize(300, 300);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        // 创建 内容面板,使用 边界布局
        JPanel panel = new JPanel(new BorderLayout());

        // 创建 一个工具栏实例
        JToolBar toolBar = new JToolBar("测试工具栏");

        // 创建 工具栏按钮
        JButton previousBtn = new JButton(new ImageIcon("previous.png"));
        JButton pauseBtn = new JButton(new ImageIcon("pause.png"));
        JButton nextBtn = new JButton(new ImageIcon("next.png"));

        // 添加 按钮 到 工具栏
        toolBar.add(previousBtn);
        toolBar.add(pauseBtn);
        toolBar.add(nextBtn);

        // 创建一个文本区域,用于输出相关信息
        final JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);

        // 添加 按钮 的点击动作监听器,并把相关信息输入到 文本区域
        previousBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append("上一曲\n");
            }
        });
        pauseBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append("暂停\n");
            }
        });
        nextBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.append("下一曲\n");
            }
        });

        // 添加 工具栏 到 内容面板 的 顶部
        panel.add(toolBar, BorderLayout.PAGE_START);
        // 添加 文本区域 到 内容面板 的 中间
        panel.add(textArea, BorderLayout.CENTER);

        jf.setContentPane(panel);
        jf.setVisible(true);
     }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65

结果展示:

#swing
上次更新: 2024-09-19 12:17:39

← JMenuBar(菜单栏) JPopupMenu(弹出菜单)→

Copyright © 2020-2025 wuzhiyong
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式