Module java.desktop
Package javax.swing

Class 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或控件的组件。有关示例和使用工具栏的信息,请参阅如何使用工具栏,这是Java教程中的一个部分。

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

警告: Swing不是线程安全的。更多信息请参见Swing的线程策略

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

自从:
1.2
另请参阅:
  • Constructor Details

    • 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
  • Method Details

    • 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已更改。调用以使用最新版本从UIFactory替换UI。
      覆盖:
      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 - 如果为true,则绘制边框
      另请参阅:
    • 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)
      设置此工具栏的鼠标悬停状态。如果鼠标悬停状态为true,则仅在鼠标指针悬停在按钮上时才绘制工具栏按钮的边框。此属性的默认值为false。

      外观和感觉的实现可能选择忽略此属性。

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

      public boolean isRollover()
      返回鼠标悬停状态。
      返回:
      如果要绘制鼠标悬停工具栏按钮,则为true;否则为false
      自:
      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