Swing组件JToolBar的介绍及使用

JToolBar(工具栏)

  • 说明
    ToolBar的功能是用来放置各种常用的功能或控制组件,这个功能在各类软件中都可以很轻易的看到.一般我们在设计软件时,会将 所有功能依类放置在菜单中(JMenu),但当功能数量相当多时,可能造成用户操作一个简单的操作就必须繁复的寻找菜单中相关的功能 ,这将造成用户操作上的负担.若我们能将一般常用的功能以工具栏方式呈现在菜单下,让用户很快得到他想要的功能,不仅增加用户 使用软件的意愿,也加速工作的运行效率.

  • 使用

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

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

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

JToolBar(String name)

JToolBar(int orientation)

JToolBar(String name, int orientation)

  • 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);
     }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值