Javafx 简明教程

JavaFX - ToolBar

ToolBar 是一种图形用户界面控件,具有按钮、菜单或其他控件的水平或垂直条带。它通常用于快速访问常用的命令或功能。我们可以将工具栏放置在任何应用程序或网页的顶部,如下图所示:

A ToolBar is a graphical user interface control with horizontal or vertical strip of buttons, menus, or other controls. It is often used to provide quick access to frequently used commands or functions. We can locate a toolbar at top of any application or web page as shown in the below figure −

toolbar

ToolBar in JavaFX

在 JavaFX 中,工具栏由 javafx.scene.control 类表示。此类是名为 @{s15} 的包的一部分。通过实例化此类,可以在 JavaFX 中创建一个 ToolBar 节点。它具有以下构造函数:

In JavaFX, the tool bar is represented by the ToolBar class. This class is a part of the package named javafx.scene.control. By instantiating this class, we can create a ToolBar node in JavaFX. It has the following constructors −

  1. ToolBar() − It is the default constructor used to create a tool bar without any predefined nodes.

  2. ToolBar(Node nodes) − It is the parameterized constructor of ToolBar class which creates a new tool bar with the specified nodes.

Steps to create a tool bar in JavaFX

按照以下步骤在 JavaFX 中创建一个工具栏。

Follow the steps given below to create a tool bar in JavaFX.

Step 1: Create nodes to display within the ToolBar

首先,我们需要创建一个要在 ToolBar 中显示的节点列表,其中,每个节点代表一个不同的命令或函数。在这里,我们将通过使用以下代码块创建按钮、分隔符和文本字段 -

First, we need to create a list of nodes to display within the ToolBar where, each node represents a distinct command or function. Here, we are going to create buttons, separator and text fields by using the below code blocks −

// Creating buttons and a text field
Button btnNew = new Button("New");
Button btnOpen = new Button("Open");
Button btnSave = new Button("Save");
Button btnExit = new Button("Exit");
TextField txtSearch = new TextField();
txtSearch.setPromptText("Search");
// creating separators
Separator sepOne = new Separator();
Separator sepTwo = new Separator();

Step 2: Instantiate the ToolBar class

如前所述,工具栏是使用名为 ToolBar 的类创建的。因此,如以下代码块所示实例化此类 -

As mentioned earlier, a tool bar is created using the class named ToolBar. Therefore, instantiate this class as shown in the following code block −

// creating a toolbar
ToolBar toolsB = new ToolBar();

Step 3: Add the nodes to the ToolBar object

要将所有节点添加到 ToolBar,我们使用 getItems()addAll() 方法。以下给出的代码对此进行了演示 -

To add all the nodes to the ToolBar, we use the getItems() and addAll() methods. The code given below demonstrates it−

// adding the nodes
toolsB.getItems().addAll(btnNew, btnOpen, btnSave, sepOne, txtSearch, sepTwo, btnExit);

Step 4: Launching Application

创建工具栏并添加所有节点后,按照以下给定的步骤正确启动应用程序 -

Once the tool bar is created and all the nodes are added, follow the given steps below to launch the application properly −

  1. Firstly, instantiate the class named BorderPane by passing the ToolBar object as a parameter value to its constructor. However, we can use any of the layout panes such as GridPane or StackPane.

  2. Then, instantiate the class named Scene by passing the BorderPane object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor.

  3. Then, set the title to the stage using the setTitle() method of the Stage class.

  4. Now, a Scene object is added to the stage using the setScene() method of the class named Stage.

  5. Display the contents of the scene using the method named show().

  6. Lastly, the application is launched with the help of the launch() method.

Example

以下是将使用 JavaFX 创建 ToolBar 的程序。将此代码保存在名为 ToolbarExample.java 的文件中。

Following is the program that will create a ToolBar using JavaFX. Save this code in a file with the name ToolbarExample.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ToolbarExample extends Application {
   @Override
   public void start(Stage stage) throws Exception {
      // Creating buttons and a text field
      Button btnNew = new Button("New");
      Button btnOpen = new Button("Open");
      Button btnSave = new Button("Save");
      Button btnExit = new Button("Exit");
      TextField txtSearch = new TextField();
      txtSearch.setPromptText("Search");
      // creating separators
      Separator sepOne = new Separator();
      Separator sepTwo = new Separator();
      // creating a toolbar
      ToolBar toolsB = new ToolBar();
      // adding the nodes
      toolsB.getItems().addAll(btnNew, btnOpen, btnSave, sepOne, txtSearch, sepTwo, btnExit);
      // Creating a BorderPane as root
      BorderPane root = new BorderPane();
      // adding the ToolBar at the top
      root.setTop(toolsB);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(root, 400, 300);
      stage.setTitle("ToolBar in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

使用以下命令从命令提示符处编译并执行已保存的 Java 文件。

Compile and execute the saved Java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls ToolbarExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ToolbarExample

执行以上程序后,会生成一个 JavaFX 窗口,显示带有指定按钮和文本字段的 ToolBar,如下所示。

On executing, the above program generates a JavaFX window displaying a ToolBar with specified buttons and text fields as shown below.

toolbar output

Setting the Orientation of the ToolBar

要更改 ToolBar 的方向,我们可以使用 setOrientation() 方法并传递 Orientation.HORIZONTALOrientation.VERTICAL 作为参数。

To change the orientation of the ToolBar, we can use the setOrientation() method and pass either Orientation.HORIZONTAL or Orientation.VERTICAL as the argument.

Example

在以下 JavaFX 程序中,我们将创建一个垂直 ToolBar。将此代码保存在名为 VerticalToolbar.java 的文件中。

In the following JavaFX program, we will create a vertical ToolBar. Save this code in a file with the name VerticalToolbar.java.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Separator;
import javafx.scene.control.TextField;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.geometry.Orientation;
public class VerticalToolbar extends Application {
   @Override
   public void start(Stage stage) throws Exception {
      // Creating buttons
      Button btnNew = new Button("New");
      Button btnOpen = new Button("Open");
      Button btnSave = new Button("Save");
      Button btnExit = new Button("Exit");
      // creating separators
      Separator sepOne = new Separator();
      Separator sepTwo = new Separator();
      Separator sepThree = new Separator();
      // creating a toolbar
      ToolBar toolsB = new ToolBar();
      // adding the nodes
      toolsB.getItems().addAll(btnNew, sepOne, btnOpen, sepTwo, btnSave, sepThree, btnExit);
      toolsB.setOrientation(Orientation.VERTICAL);
      // Creating a BorderPane as root
      BorderPane root = new BorderPane();
      // adding the ToolBar at the top
      root.setTop(toolsB);
      // Create a Scene and set it to the Stage
      Scene scene = new Scene(root, 400, 300);
      stage.setTitle("ToolBar in JavaFX");
      stage.setScene(scene);
      stage.show();
   }
   public static void main(String[] args) {
      launch(args);
   }
}

使用以下命令从命令提示符处编译并执行已保存的 Java 文件。

Compile and execute the saved Java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls VerticalToolbar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls VerticalToolbar

当我们执行上述代码时,它将生成以下输出。

When we execute the above code, it will generate the following output.

toolbar output2