A toolbar can be either horizontal or vertical. When the orientation is horizontal, the objects in the toolbar are displayed left-to-right. When the orientation is vertical, the objects in the toolbar are displayed top-to-bottom. This orientation is automatically changed when the toolbar is moved to the top or side of a container.

    // Create a horizontal toolbar
JToolBar toolbar = new JToolBar(); // Create a vertical toolbar
toolbar = new JToolBar(null, JToolBar.VERTICAL); // Get current orientation
int orient = toolbar.getOrientation();

The following code adds various buttons to the toolbar:

    // Get icon
ImageIcon icon = new ImageIcon("icon.gif"); // Create an action with an icon
Action action = new AbstractAction("Button Label", icon) {
// This method is called when the button is pressed
public void actionPerformed(ActionEvent evt) {
// Perform action
}
}; // Add a button to the toolbar; remove the label and margin before adding
JButton c1 = new JButton(action);
c1.setText(null);
c1.setMargin(new Insets(0, 0, 0, 0));
toolbar.add(c1); // Add a toggle button; remove the label and margin before adding
JToggleButton c2 = new JToggleButton(action);
c2.setText(null);
c2.setMargin(new Insets(0, 0, 0, 0));
toolbar.add(c2); // Add a combobox
JComboBox c3 = new JComboBox(new String[]{"A", "B", "C"});
c3.setPrototypeDisplayValue("XXXXXXXX"); // Set a desired width
c3.setMaximumSize(c3.getMinimumSize());
toolbar.add(c3);
// See also e765 监听组合框中选择不同的项

If the toolbar is to be floatable (see e818 防止工具栏可移动), it must be added to a container with a BorderLayout.

    // Add the toolbar to a frame
JFrame frame = new JFrame();
frame.getContentPane().add(toolbar, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
Related Examples

e816. 创建工具栏的更多相关文章

  1. 用CToolBarCtrl类为对话框创建工具栏

    ---恢复内容开始--- 首先CToolBarCtrl类内部维护了三个重要的数据结构:一个图像列表,一个字符串列表,一个TBBUTTON结构体的列表. 知道了这一点,下面的理解起来就轻松了.慢慢来: ...

  2. wxpython 创建工具栏和菜单栏

    下面看一下关于创建工具栏,状态栏和菜单的方法,看下面一个例子: import wx class ToolBarFrame(wx.Frame): def __init__(self,parent,id) ...

  3. win32创建工具栏的自定义图标

    注意点:使用LoadImage函数加载bmp图片,这里特指BMP图片,其实LoadImage可以加载很多格式的图片 HBITMAP bitmap = (HBITMAP)LoadImage((HINST ...

  4. Android 学习笔记四:创建工具栏按钮

    原文:http://blog.csdn.net/lihongxun945/article/details/48951199 前面我们已经可以在一个Activity中添加一些按钮之类的组件.由于手机的屏 ...

  5. 雷林鹏分享:jQuery EasyUI 数据网格 - 创建复杂工具栏

    jQuery EasyUI 数据网格 - 创建复杂工具栏 数据网格(datagrid)的工具栏(toolbar)可以包含按钮及其他组件. 您可以通个一个已存在的 DIV 标签来简单地定义工具栏布局,该 ...

  6. MFC工具栏的创建、设计与使用实例

    本文通过实例说明MFC工具栏的创建.设计和使用方法,包括三个demo.       demo1:创建一个工具栏 C++代码 //摘抄自MSDN demo1 (创建一个工具栏) 1.Create a t ...

  7. VS2010/MFC编程入门之三十七(工具栏:工具栏的创建、停靠与使用)

    鸡啄米在上一节教程中讲了工具栏资源及CToolBar类,本节继续讲解工具栏的相关知识,主要内容包括工具栏的创建.停靠与使用. 工具栏的使用 上一节中鸡啄米提到过,一般情况下工具栏中的按钮在菜单栏中都有 ...

  8. VS2010-MFC(工具栏:工具栏的创建、停靠与使用)

    转自:http://www.jizhuomi.com/software/217.html 上一节教程讲了工具栏资源及CToolBar类,本节继续讲解工具栏的相关知识,主要内容包括工具栏的创建.停靠与使 ...

  9. 【Windows编程】系列第六篇:创建Toolbar与Statusbar

    上一篇我们学习了解了如何使用Windows GDI画图,该应用程序都是光光的静态窗口,我们使用Windows应用程序,但凡稍微复杂一点的程序都会有工具栏和状态栏,工具栏主要用于一些快捷功能按钮.比如典 ...

随机推荐

  1. (原创)c++11中的日期和时间库

    c++11提供了日期时间相关的库chrono,通过chrono相关的库我们可以很方便的处理日期和时间.c++11还提供了字符串的宽窄转换功能,也提供了字符串和数字的相互转换的库.有了这些库提供的便利的 ...

  2. (原创)C++11改进我们的程序之简化我们的程序(五)

    这次要讲的是:c++11中的bind和function std::function 它是函数.函数对象.函数指针.和成员函数的包装器,可以容纳任何类型的函数对象,函数指针,引用函数,成员函数的指针.以 ...

  3. canvas的图片绘制案例

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...

  4. 什么是内存溢出以及java中内存泄漏5种情况的总结

    内存泄漏定义(memory leak):一个不再被程序使用的对象或变量还在内存中占有存储空间. 一次内存泄漏似乎不会有大的影响,但内存泄漏堆积后的后果就是内存溢出.内存溢出 out of memory ...

  5. GreenDAO - primary key on multiple columns

    转:http://stackoverflow.com/questions/15250609/greendao-primary-key-on-multiple-columns Does GreenDAO ...

  6. 使用 Jackson 树模型(tree model) API 处理 JSON

    http://blog.csdn.net/gao1440156051/article/details/54091702 http://blog.csdn.net/u010003835/article/ ...

  7. Android Studio preview 不显示,程序运行正常

    答案来自 stack flow 修改: res -> values -> style.xml style name="AppTheme" parent="Ba ...

  8. JAVA-JSP内置对象之application对象获得服务器版本

    相关资料:<21天学通Java Web开发> application对象获得服务器版本1.通过application对象的getMajorVersion()方法和getMinorVersi ...

  9. Node.js学习笔记(1)--一个最简单的服务器请求

    说明(2017-5-2 10:27:03): 1. 需要安装node,http://nodejs.cn/download/ 2. 安装完后,在cmd里输入node -v可以查看版本. 3. 代码foo ...

  10. <跟股市谚语学炒股> 读书笔记

    书在这里 一般情况下,当成交清单上显示的买盘金额大.笔数少,卖盘金额小.笔数多时,系主力在建仓.散户在卖出:相反,若买盘金额小.笔数多,卖盘金额大.笔数少时,系主力在出货.散户在买入 一般来说,当大盘 ...