ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## 创建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 张小图片作为按钮的图标,如下: ![](https://box.kancloud.cn/1a2daf9d8ecad03a1f43980a0bb4d405_32x32.png)![](https://box.kancloud.cn/8c0706ed441254c89b379b01e8ddfd59_32x32.png)![](https://box.kancloud.cn/e7078e9ce225424c89fc0f1d47a1feb1_32x32.png) 分别命名为: 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); } } ``` 结果展示: ![](https://box.kancloud.cn/3b5e0fffd98ea70acda922f00baf38da_450x400.png)