模块 java.desktop

类 JToolBar

所有已实现的接口:
ImageObserver , MenuContainer , Serializable , Accessible , SwingConstants

@JavaBean (defaultProperty ="UI", description ="A component which displays commonly used controls or Actions.") public class JToolBar extends JComponent implements SwingConstants , Accessible
JToolBar 提供了一个组件,可用于显示常用的 Action 或控件。有关使用工具栏的示例和信息,请参阅 如何使用工具栏The Java Tutorial 中的一个部分。

对于大多数外观,用户可以将工具栏拖出到单独的窗口中(除非 floatable 属性设置为 false )。为了使拖出正常工作,建议您将 JToolBar 实例添加到布局管理器为 BorderLayout 的容器的四个“边”之一,并且不要将子实例添加到其他四个“边”中的任何一个。

Warning: Swing 不是线程安全的。有关详细信息,请参阅 Swing 的线程策略

Warning: 此类的序列化对象将与未来的 Swing 版本不兼容。当前的序列化支持适用于运行相同版本 Swing 的应用程序之间的短期存储或 RMI。从 1.4 开始,对所有 JavaBeans 的长期存储的支持已添加到 java.beans 包中。请参阅 XMLEncoder

自从:
1.2
参见:
  • 构造方法详细信息

    • JToolBar

      public JToolBar()
      创建一个新的工具栏;方向默认为 HORIZONTAL
    • JToolBar

      public JToolBar(int orientation)
      使用指定的 orientation 创建一个新工具栏。 orientation 必须是 HORIZONTALVERTICAL
      参数:
      orientation - 所需的方向
    • JToolBar

      public JToolBar(String  name)
      使用指定的 name 创建一个新工具栏。该名称用作未停靠工具栏的标题。默认方向是 HORIZONTAL
      参数:
      name - 工具栏的名称
      自从:
      1.3
    • JToolBar

      public JToolBar(String  name, int orientation)
      创建具有指定 nameorientation 的新工具栏。所有其他构造函数调用此构造函数。如果 orientation 是无效值,将抛出异常。
      参数:
      name - 工具栏的名称
      orientation - 初始方向——它必须是 HORIZONTALVERTICAL
      抛出:
      IllegalArgumentException - 如果方向既不是 HORIZONTAL 也不是 VERTICAL
      自从:
      1.3
  • 方法详情

    • getUI

      public ToolBarUI  getUI()
      返回工具栏的当前 UI。
      重写:
      getUI 在类 JComponent
      返回:
      工具栏的当前 UI。
      参见:
    • setUI

      @BeanProperty (hidden =true, visualUpdate =true, description ="The UI object that implements the Component\'s LookAndFeel.") public void setUI(ToolBarUI  ui)
      设置呈现此组件的 L&F 对象。
      参数:
      ui - ToolBarUI L&F 对象
      参见:
    • updateUI

      public void updateUI()
      来自 UIFactory 的通知,L&F 已更改。调用以将 UI 替换为来自 UIFactory 的最新版本。
      重写:
      updateUI 在类 JComponent
      参见:
    • getUIClassID

      @BeanProperty (bound =false) public String  getUIClassID()
      返回呈现此组件的 L&F 类的名称。
      重写:
      getUIClassID 在类 JComponent
      返回:
      字符串“ToolBarUI”
      参见:
    • getComponentIndex

      public int getComponentIndex(Component  c)
      返回指定组件的索引。 (注意:分隔符占据索引位置。)
      参数:
      c - 要查找的 Component
      返回:
      一个整数,指示组件的位置,其中 0 是第一个
    • getComponentAtIndex

      public Component  getComponentAtIndex(int i)
      返回指定索引处的组件。
      参数:
      i - 组件的位置,其中 0 是第一个
      返回:
      该位置的 Component,或无效索引的 null
    • setMargin

      @BeanProperty (expert =true, description ="The margin between the tool bar\'s border and contents") public void setMargin(Insets  m)
      设置工具栏边框与其按钮之间的边距。设置为 null 会导致工具栏使用默认边距。工具栏的默认 Border 对象使用此值来创建适当的边距。但是,如果在工具栏上设置了非默认边框,则 Border 对象有责任创建适当的边距空间(否则该属性将被忽略)。
      参数:
      m - 定义边框和按钮之间空间的 Insets 对象
      参见:
    • getMargin

      public Insets  getMargin()
      返回工具栏边框与其按钮之间的边距。
      返回:
      包含边距值的 Insets 对象
      参见:
    • isBorderPainted

      public boolean isBorderPainted()
      获取 borderPainted 属性。
      返回:
      borderPainted 属性的值
      参见:
    • setBorderPainted

      @BeanProperty (expert =true, description ="Does the tool bar paint its borders?") public void setBorderPainted(boolean b)
      设置 borderPainted 属性,如果应绘制边框,则为 true。此属性的默认值为 true 。某些外观可能无法实现彩绘边框;他们将忽略此属性。
      参数:
      b - 如果为真,则绘制边框
      参见:
    • paintBorder

      protected void paintBorder(Graphics  g)
      如果 borderPainted 属性为 true,则绘制工具栏的边框。
      重写:
      paintBorder 在类 JComponent
      参数:
      g - 完成绘画的 Graphics 上下文
      参见:
    • isFloatable

      public boolean isFloatable()
      获取 floatable 属性。
      返回:
      floatable 属性的值
      参见:
    • setFloatable

      @BeanProperty (preferred =true, description ="Can the tool bar be made to float by the user?") public void setFloatable(boolean b)
      设置 floatable 属性,该属性必须为 true 以便用户移动工具栏。通常,可以将可浮动工具栏拖到同一容器内的不同位置或拖出到它自己的窗口中。此属性的默认值为 true 。有些外观可能无法实现可浮动的工具栏;他们将忽略此属性。
      参数:
      b - 如果是 true ,工具栏可以移动; false否则
      参见:
    • getOrientation

      public int getOrientation()
      返回工具栏的当前方向。该值为 HORIZONTALVERTICAL
      返回:
      表示当前方向的整数——HORIZONTALVERTICAL
      参见:
    • setOrientation

      @BeanProperty (preferred =true, enumerationValues ={"SwingConstants.HORIZONTAL","SwingConstants.VERTICAL"}, description ="The current orientation of the tool bar") public void setOrientation(int o)
      设置工具栏的方向。方向必须具有值 HORIZONTALVERTICAL 。如果 orientation 是无效值,将抛出异常。
      参数:
      o - 新方向 - HORIZONTALVERTICAL
      抛出:
      IllegalArgumentException - 如果方向既不是 HORIZONTAL 也不是 VERTICAL
      参见:
    • setRollover

      @BeanProperty (preferred =true, visualUpdate =true, description ="Will draw rollover button borders in the toolbar.") public void setRollover(boolean rollover)
      设置此工具栏的翻转状态。如果翻转状态为真,则仅当鼠标指针悬停在工具栏按钮上时才会绘制工具栏按钮的边框。此属性的默认值为 false。

      外观的实现可能会选择忽略此属性。

      参数:
      rollover - 鼠标悬停工具栏按钮为真;否则为假
      自从:
      1.4
    • isRollover

      public boolean isRollover()
      返回翻转状态。
      返回:
      如果要绘制悬停工具栏按钮,则为 true;否则为假
      自从:
      1.4
      参见:
    • addSeparator

      public void addSeparator()
      将默认大小的分隔符附加到工具栏的末尾。默认大小由当前的外观决定。
    • addSeparator

      public void addSeparator(Dimension  size)
      将指定大小的分隔符附加到工具栏的末尾。
      参数:
      size - 分隔符的Dimension
    • add

      public JButton  add(Action  a)
      添加一个新的 JButton 来调度操作。
      参数:
      a - 添加为新菜单项的 Action 对象
      返回:
      调度动作的新按钮
    • createActionComponent

      protected JButton  createActionComponent(Action  a)
      为添加到 JToolBarAction 创建 JButton 的工厂方法。如果传递了 null 操作,则默认名称为空。
      参数:
      a - 要添加的按钮的 Action
      返回:
      新创建的按钮
      自从:
      1.3
      参见:
    • createActionChangeListener

      protected PropertyChangeListener  createActionChangeListener(JButton  b)
      返回一个正确配置的 PropertyChangeListener,它会在 Action 发生更改时更新控件,如果需要控件的默认属性更改监听器,则返回 null
      参数:
      b - 一个 JButton
      返回:
      null
    • addImpl

      protected void addImpl(Component  comp, Object  constraints, int index)
      如果添加了 JButton,它最初设置为禁用。
      重写:
      addImpl 在类 Container
      参数:
      comp - 要增强的组件
      constraints - 要在组件上强制执行的约束
      index - 组件的索引
      参见:
    • paramString

      protected String  paramString()
      返回此 JToolBar 的字符串表示形式。此方法仅用于调试目的,返回字符串的内容和格式可能因实现而异。返回的字符串可能为空,但可能不是 null
      重写:
      paramString 在类 JComponent
      返回:
      这个 JToolBar 的字符串表示。
    • getAccessibleContext

      @BeanProperty (bound =false) public AccessibleContext  getAccessibleContext()
      获取与此 JToolBar 关联的 AccessibleContext。对于工具栏,AccessibleContext 采用 AccessibleJToolBar 的形式。如有必要,将创建一个新的 AccessibleJToolBar 实例。
      指定者:
      getAccessibleContext 在接口 Accessible
      重写:
      getAccessibleContext 在类 Component
      返回:
      作为此 JToolBar 的 AccessibleContext 的 AccessibleJToolBar