## 创建JToolBar组件
有四个构造函数可以用来创建JToolBar组件:
```
public JToolBar()
JToolBar jToolBar = new JToolBar();
public JToolBar(int orientation)
JToolBar jToolBar = new JToolBar(JToolBar.VERTICAL);
public JToolBar(String name)
JToolBar jToolBar = new JToolBar("Window Title");
public JToolBar(String name,int orientation)
JToolBar jToolBar = new JToolBar("Window Title", ToolBar.VERTICAL);
```
在默认情况下,工具栏是以水平方向进行创建的。然而,我们可以通过JToolBar的常量HORIZONTAL与VERTICAL显示指定方向。
而且在默认情况下,工具栏是可以浮动的。所以,如果我们使用水平方向创建一个工具栏,用户可以在窗口周围拖动工具栏来改变工具栏的方向。
## 向JToolBar添加组件
一旦我们拥有一个JToolBar,我们需要向其中添加组件。任意的Component都可以添加到工具栏。当处理水平工具栏时,由于美观的原因,如果工具栏的组件是大致相同的高度时是最好的。对于垂直工具栏,如果工具栏组件具有大致相同的宽度则是最好的。JToolBar类只定义了一个方法用于添加工具栏项目;其他的方法,例如add(Component)是由Container继承而来的。另外,我们可以向工具栏添加分隔符。
```
public JButton add(Action action);
public void addSeparator();
public void addSeparator(Dimension size);
```
当使用JToolBar的add(Action)方法时,所添加的Action被封闭在一个JButton对象中。这与向JMenu或是JPopupMenu组件添加Action不同,在后一种情况中,所添加的是JMenuItem对象。对于JMenu与JPopupMenu,以这种方式添加Action是类的Javadoc中所不推荐的。对于分隔符,如果我们没有指定尺寸,所安装的观感会强制默认的尺寸设置。
由工具栏移除组件可以使用下面的方法:
```
public void remove(Component component)
```
## 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)
```
## 代码实例
本实例需要用到 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);
}
}
```
结果展示:

- 前言
- CSS
- VUE
- Vue.js 安装
- Vue.js 目录结构
- Vue.js 起步
- Vue.js 模板语法
- Vue.js 条件与循环
- Vue.js 循环语句
- Vue.js 计算属性
- Vue.js 监听属性
- Vue.js 样式绑定
- Vue.js 事件处理器
- Vue.js 表单
- Vue.js 组件
- Vue.js 自定义指令
- Vue.js 路由
- React
- 安装
- React JSX
- React 组件
- 问题1
- React state
- React Props
- React 组件 API
- React 组件生命周期
- React AJAX
- React 表单与事件
- React Refs
- Babel
- Ant Design
- 安装
- 快速上手
- webpack
- 安装
- JavaScript
- 知识点
- 字符转数字
- js中字符串全部替换
- 函数
- reduce() 方法
- UI控件
- DataTable
- 语言配置 选项
- 增加行
- 列渲染-自定义列
- 创建行回调-操作行
- 自定义数据长度
- 默认设置
- 样式
- 集成Bootstrap 3
- 分页相关
- 数据
- NodeJs
- Electron
- 打包
- 介绍
- 知识点
- 使用 jquery
- CommonJS规范
- Bower
- 简介
- 安装
- Swing
- Swing界面组件
- JComboBox
- JDesktopPane和JInternalFrame
- JFrame
- JTabbedPane
- JTable
- JProgressBar
- JToolBar
- 知识点
- 截取log4j日志并输出到GUI组件
- JFrame 居中显示
- Swing中三种最大化初始窗口的方法
- Layout布局
- BorderLayout
- GridBagLayout
- GridLayout
- BoxLayout
- JxBrowser
- 浏览器引擎-Browser Engine
- 创建浏览器-Creating Browser
- 创建隐身浏览器-Creating Incognito Browser
- 存储用户数据-Storing User Data
- 处理浏览器-Disposing Browser
- 浏览器偏好-Browser Preferences
- 恢复浏览器-Restoring Browser
- 渲染流程事件-Render Process Events
- 渲染进程ID-Render Process ID
- 获取帧ID-Getting Frame IDs
- 获取产品版本-Getting Product Version
- 寻找文本-Finding Text
- 清除缓存-Clearing Cache
- 转发键盘事件-Forwarding Key Events
- 转发鼠标事件-Forwarding Mouse Events
- 加载内容-Loading Content
- 加载网址-Loading URL
- 使用POST加载URL-Loading URL with POST
- 加载HTML-Loading HTML
- 从JAR加载HTML-Loading HTML from JAR
- 获取HTML-Getting HTML
- 获取选定的HTML-Getting Selected HTML
- 加载事件-Loading Events
- 正在加载和等待-Loading & Waiting
- 显示PDF-Displaying PDF
- 网络活动-Network Events
- 处理资源加载-Handling Resources Loading
- 启用/禁用退格导航-Enabling/Disabling Backspace Navigation
- 处理SSL证书错误-Handling SSL Certificate Errors
- SSL证书验证程序-SSL Certificate Verifier
- 导航历史-Navigation History
- User-Agent
- WebSockets
- 处理加载-Handling Loading
- 修改POST / PUT / PATCH上传数据-Modifying POST/PUT/PATCH Upload Data
- HTML5本地和会话存储-HTML5 Local & Session storages
- 访问HTTP响应数据-Accessing HTTP response data
- HTTP服务器白名单-HTTP Server Whitelist
- 自定义协议处理程序-Custom Protocol Handler
- ActiveX
- 浏览器视图-Browser View
- 轻量级或重量级-Lightweight or Heavyweight
- 在Swing中使用JxBrowser-Using JxBrowser in Swing
- 在JavaFX中使用JxBrowser-Using JxBrowser in JavaFX
- 在SWT中使用JxBrowser-Using JxBrowser in SWT
- 自定义CSS光标-Custom CSS Cursors
- 标题事件-Title Events
- 状态事件-Status Events
- 键盘和鼠标事件-Keyboard & Mouse Events
- 处理键盘事件-Handling Keyboard Events
- 处理鼠标事件-Handling Mouse Events
- 编辑器命令-Editor Commands
- 拖放-Drag & Drop
- 内容缩放-Content scaling
- 上下文菜单-Context Menu
- JMenuBar
- JInternalFrame
- JTabbedPane
- JPanel
- 加速轻量级渲染-Accelerated Lightweight Rendering
- 透明背景-Transparent Background
- DOM
- 使用文档-Working with Document
- 注入css-Injecting CSS
- 寻找元素-Finding Elements
- 元素属性-Element Attributes
- 创建元素和文本节点-Creating Element & Text Node
- 设置节点值-Setting Node Value
- Select & Option Elements
- 选择CheckBox-Selecting CheckBox
- Getting Selected Text
- 模拟点击-Simulating Click
- DOM事件
- XPath
- 查询选择器-Query Selector
- 使用表单-Working with Form
- 滚动文档-Scrolling Document
- 在Point处查找节点-Finding Node at Point
- 获得元素界限-Getting Element Bounds
- 监听内容变化-Listening to the Сontent Сhanges
- 模拟DOM事件-Simulating DOM Events
- Audio & Video
- MP3/MP4/H.264
- 网络摄像头和麦克风-Web Camera & Microphone
- 全屏视频-Full Screen Video
- 静音音频-Muting Audio
- HTML5 Video
- Pop-ups
- 关于弹出窗口-About Pop-ups
- 在swing中处理弹出窗口-Handling Pop-ups Swing
- 在JavaFX中处理弹出窗口-Handling Pop-ups JavaFX
- Dialogs
- JavaScript对话框-JavaScript Dialogs
- 文件下载-File Download
- 上传文件-File Upload
- 选择SSL证书-Select SSL Certificate
- 选择自定义SSL证书-Select Custom SSL Certificate
- 卸载前-Before Unload
- 颜色选择器-Color Chooser
- Proxy
- 使用代理-Working with Proxy
- 系统代理设置-System Proxy Settings
- Authentication
- 处理代理验证-Handling Proxy Authentication
- 处理基本,摘要和NTLM身份验证-Handling Basic, Digest and NTLM Authentication
- JavaScript Java Bridge
- 从Java调用JavaScript-Calling JavaScript from Java
- 从JavaScript调用Java-Calling Java from JavaScript
- 控制台消息-Console Messages
- 使用JSON-Working with JSON
- 使用jQuery-Working with jQuery
- 使用ScriptContext-Working with ScriptContext
- 将表单数据发送到Java-Sending Form Data to Java
- 使用数组-Working with Arrays
- @JSAccessible
- Plugins
- Printing
- Cookies
- Saving Web Page
- Zoom
- Integration
- Deploying
- Chromium
- Spell Checker
- Debugging
- Why JxBrowser
- Tips & Tricks
- 基础知识
- AbstractAction
- Void
- SwingWorker应用详解
- JAVA实现国际化
- UIManager
- AppJS
- heX
- bootstrap
- 知识点
- 空行
- Eclipse RCP
- Eclipse e4 概览