## 創建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 概覽