如何使用工具栏
JToolBar是将多个组件(通常带有图标的buttons)分组为行或列的容器。通常,工具栏可让您轻松访问menus中的功能。 如何使用动作描述了如何在菜单项和工具栏按钮中提供相同的功能。
下图显示了名为ToolBarDemo
的应用程序,该应用程序在文本区域上方包含一个工具栏。单击启动按钮以使用Java™Web 开始(下载 JDK 7 或更高版本)运行 ToolBarDemo。另外,要自己编译并运行它,请查阅example index。
默认情况下,用户可以将工具栏拖动到其容器的另一边缘或拖出其自己的窗口。下图显示了用户将工具栏拖动到其容器的右边缘后,应用程序的外观。
为了使拖动行为正确运行,工具栏必须位于使用BorderLayout布局 管理 器的容器中。工具栏影响的组件通常位于容器的中心。工具栏必须是容器中唯一的其他组件,并且不能位于中间。
下图显示了用户将工具栏拖动到其窗口之外后,应用程序的外观。
以下代码创建工具栏并将其添加到容器中。您可以在ToolBarDemo.java中找到整个程序。
public class ToolBarDemo extends JPanel
implements ActionListener {
...
public ToolBarDemo() {
super(new BorderLayout());
...
JToolBar toolBar = new JToolBar("Still draggable");
addButtons(toolBar);
...
setPreferredSize(new Dimension(450, 130));
add(toolBar, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
}
...
}
该代码通过将两个组件都放置在由边框布局控制的面板中,使工具栏位于滚动窗格上方,工具栏位于PAGE_START
位置,滚动窗格位于CENTER
位置。因为滚动窗格位于中间,并且容器中没有除工具栏之外的其他组件,所以默认情况下可以将工具栏拖动到容器的其他边缘。也可以将工具栏拖到其自己的窗口中,在这种情况下,该窗口的标题为“仍可拖动”,如JToolBar
构造函数所指定。
创建工具栏按钮
工具栏中的按钮是普通的JButton
实例,它们使用 Java Look and Feel Graphics Repository 中的图像。如果您的工具栏具有 Java 外观,请使用Java 外观图形存储库中的图像。
这是创建按钮并将其添加到工具栏的代码。
protected void addButtons(JToolBar toolBar) {
JButton button = null;
//first button
button = makeNavigationButton("Back24", PREVIOUS,
"Back to previous something-or-other",
"Previous");
toolBar.add(button);
//second button
button = makeNavigationButton("Up24", UP,
"Up to something-or-other",
"Up");
toolBar.add(button);
...//similar code for creating and adding the third button...
}
protected JButton makeNavigationButton(String imageName,
String actionCommand,
String toolTipText,
String altText) {
//Look for the image.
String imgLocation = "images/"
+ imageName
+ ".gif";
URL imageURL = ToolBarDemo.class.getResource(imgLocation);
//Create and initialize the button.
JButton button = new JButton();
button.setActionCommand(actionCommand);
button.setToolTipText(toolTipText);
button.addActionListener(this);
if (imageURL != null) { //image found
button.setIcon(new ImageIcon(imageURL, altText));
} else { //no image found
button.setText(altText);
System.err.println("Resource not found: " + imgLocation);
}
return button;
}
第一次调用makeNavigationButton
时,将使用图形存储库中的 24x24“后退”导航图像为第一个按钮创建图像。
除了找到按钮的图像之外,makeNavigationButton
方法还创建按钮,为其操作命令和工具提示文本设置字符串,并为按钮添加操作侦听器。如果缺少图像,则该方法将打印一条错误消息并向该按钮添加文本,以便该按钮仍然可用。
Note:
如果工具栏中的任何按钮都与其他组件(例如菜单项)的功能重复,则您可能应该按照如何使用动作所述创建并添加工具栏按钮。
自定义工具栏
通过在前面的示例中添加几行代码,我们可以演示更多工具栏功能:
-
使用
setFloatable(false)
固定工具栏。 -
当用户用光标越过工具栏按钮时,使用
setRollover(true)
直观地指示它们。 -
在工具栏上添加分隔符。
-
将非按钮组件添加到工具栏。
您可以通过运行 ToolBarDemo2 来查看这些功能。单击启动按钮以使用Java™Web 开始(下载 JDK 7 或更高版本)运行 ToolBarDemo2.另外,要自己编译并运行它,请查阅example index。
您可以在ToolBarDemo2.java中找到该程序的完整代码。您可以在下面看到使用这些自定义功能的新 UI 的图片。
由于无法再拖动工具栏,因此它的左边缘不再有凹凸。这是关闭拖动的代码:
toolBar.setFloatable(false);
工具栏处于过渡 Pattern,因此光标下方的按钮具有可视指示器。视觉指示器的类型取决于外观。例如,Metal 外观使用渐变效果来指示光标下方的按钮,而其他类型的外观则为此使用边框。这是设置过渡 Pattern 的代码:
toolBar.setRollover(true);
上面示例中的另一个明显区别是工具栏包含两个新组件,这些组件的前面是一个称为separator的空白。这是添加分隔符的代码:
toolBar.addSeparator();
这是添加新组件的代码:
//fourth button
button = new JButton("Another button");
...
toolBar.add(button);
//fifth component is NOT a button!
JTextField textField = new JTextField("A text field");
...
toolBar.add(textField);
您可以通过调用setAlignmentY
方法轻松地使工具栏组件的顶部对齐或底部对齐,而不是居中。例如,要对齐工具栏中所有组件的顶部,请在每个组件上调用setAlignmentY(TOP_ALIGNMENT)
。同样,当工具栏为垂直时,可以使用setAlignmentX
方法指定组件的对齐方式。这种布局灵 Active 是可能的,因为工具栏使用BoxLayout
来定位其组件。有关更多信息,请参见如何使用 BoxLayout。
工具栏 API
下表列出了常用的JToolBar构造函数和方法。 JComponent 类的 API 表中列出了您可能调用的其他方法。
方法或构造函数 | Purpose |
---|---|
JToolBar() | |
JToolBar(int) JToolBar(String) JToolBar(String, int) | 创建工具栏。可选的 int 参数使您可以指定方向;默认值为HORIZONTAL 。可选的String 参数允许您指定工具栏窗口的标题(如果将其拖动到容器的外部)。 |
Component add(Component) | 将组件添加到工具栏。 您可以使用 AbstractButton 定义的setAction(Action) 方法将按钮与Action 关联。 |
void addSeparator() | 在工具栏的末尾添加分隔符。 |
void setFloatable(boolean) boolean isFloatable() | floatable 属性默认为 true,表示用户可以将工具栏拖出到单独的窗口中。要关闭工具栏拖动功能,请使用toolBar.setFloatable(false) 。某些类型的外观可能会忽略此属性。 |
void setRollover(boolean) boolean isRollover() | 默认情况下,过渡属性为 false。若要在用户用光标越过工具栏按钮时直观地指示它们,请将此属性设置为 true。某些类型的外观可能会忽略此属性。 |
使用工具栏的示例
下表列出了使用JToolBar
的示例,并指向描述这些示例的位置。
Example | Where Described | Notes |
---|---|---|
ToolBarDemo | This page | 具有仅图标按钮的基本工具栏。 |
ToolBarDemo2 | This page | 在过渡 Pattern 下演示了一个不易 Float 的工具栏,其中包含一个分隔符和一个非按钮组件。 |
ActionDemo | 如何使用动作 | 使用Action 个对象实现工具栏。 |