Javafx 简明教程
JavaFX - Button Bar
ButtonBar 是用水平布局排列按钮的容器类型。这些按钮的排列或位置取决于我们正在使用的操作系统。通常,放置在 ButtonBar 中的所有按钮都统一大小。然而,它还允许我们自定义按钮的大小和位置。一个典型的按钮栏如图所示。它包含两个按钮,分别为“是”和“否”。
A ButtonBar is a type of container that arranges buttons in a horizontal layout. The arrangement or positions of these buttons depend on the type of operating system we are working on. Generally, all the buttons placed inside a ButtonBar are uniformly sized. However, it also allows us to customize the size as well as positions of the buttons. A typical button bar looks like the below figure. It contains two buttons namely "Yes" and "No".
ButtonBar in JavaFX
在 JavaFX 中,名为 ButtonBar 的类表示一个按钮栏。此类属于包 javafx.scene.control 。我们可以通过实例化 ButtonBar 类在 JavaFX 中创建按钮栏节点。
In JavaFX, the class named ButtonBar represents a button bar. This class belongs to the package javafx.scene.control. We can create a button bar node in JavaFX by instantiating the ButtonBar class.
这个类提供了两个构造器,如下所示 −
There are two constructors available for this class, and they are as follows −
-
ButtonBar() − It is used for creating a button bar with default properties that will be specific to the operating system.
-
ButtonBar(String buttonOrder) − It will create a button bar with the specified button order.
Steps to create a Button Bar in JavaFX
要在 JavaFX 中创建按钮栏,请执行以下步骤。
To create a Button Bar in JavaFX, follow the steps given below.
Step 1: Create two or more Buttons
在 JavaFX 中,通过实例化名为 Button 的类(该类属于包 javafx.scene.control )来创建按钮。像下面那样实例化此类 −
In JavaFX, the buttons are created by instantiating the class named Button which belongs to a package javafx.scene.control. Instantiate this class as shown below −
//Creating required buttons
Button buttonOne = new Button("Back");
Button buttonTwo = new Button("Accept");
Button buttonThree = new Button("Cancel");
同样,为项目创建所需数量的按钮。
Similarly, create the required number of buttons for the project.
Step 2: Instantiate ButtonBar class
实例化包 javafx.scene.control 的 ButtonBar 类,不清向其构造器传递任何参数值,并使用 getButtons() 方法添加所有按钮。
Instantiate the ButtonBar class of package javafx.scene.control without passing any parameter value to its constructor and add all the buttons using the getButtons() method.
//Creating a ButtonBar
ButtonBar newButtonbar = new ButtonBar();
// Adding buttons to the ButtonBar
newButtonbar.getButtons().addAll(buttonOne, buttonTwo, buttonThree);
Step 3: Launching Application
创建按钮栏后,按照以下步骤来正确启动应用程序 −
After creating the button bar, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named HBox and add the button bar using getChildren() method.
-
Then, instantiate the class named Scene by passing the HBox object as a parameter value to its constructor. We can also pass dimensions of the application screen as optional parameters to this constructor.
-
Then, set the title to the stage using the setTitle() method of the Stage class.
-
Now, a Scene object is added to the stage using the setScene() method of the class named Stage.
-
Display the contents of the scene using the method named show().
-
Lastly, the application is launched with the help of the launch() method.
Example
以下是将在 JavaFX 中创建一个按钮栏的程序。将此代码保存在名为 JavafxButtonBar.java 的文件中。
Following is the program which will create a button bar in JavaFX. Save this code in a file with the name JavafxButtonBar.java.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class JavafxButtonBar extends Application {
@Override
public void start(Stage stage) {
//Creating required buttons
Button buttonOne = new Button("Back");
Button buttonTwo = new Button("Accept");
Button buttonThree = new Button("Cancel");
//Creating a ButtonBar
ButtonBar newButtonbar = new ButtonBar();
// Adding buttons to the ButtonBar
newButtonbar.getButtons().addAll(buttonOne, buttonTwo, buttonThree);
newButtonbar.setPadding(new Insets(10));
HBox box = new HBox();
box.getChildren().addAll(newButtonbar);
//Setting the stage
Scene scene = new Scene(box, 500, 250);
stage.setTitle("ButtonBar in JavaFX");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}
使用以下命令通过命令提示符编译并执行上述 Java 文件。
Compile and execute the above Java file using the command prompt with the help of following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxButtonBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxButtonBar
在执行时,上述程序将生成一个 JavaFX 窗口,其中显示了如图所示的 ButtonBar。
On executing, the above program generates a JavaFX window displaying a ButtonBar as shown below.
Creating a ButtonBar with customized Button orders
在大多数情况下,Button 的顺序由操作系统决定。但是,如果需要自定义布局,则可以使用 ButtonBar 类的 setButtonOrder() 方法。它将按钮顺序属性作为参数,并相应地排列按钮。不同操作系统的按钮顺序属性为 BUTTON_ORDER_WINDOWS、BUTTON_ORDER_MAC_OS 和 BUTTON_ORDER_LINUX。
In most cases, the order of the Buttons is determined by the operating system. But, if a custom layout is required, then, the setButtonOrder() method of the ButtonBar class can be used. It takes button order property as a parameter and arranges the buttons accordingly. The button order properties of different OS are BUTTON_ORDER_WINDOWS, BUTTON_ORDER_MAC_OS, and BUTTON_ORDER_LINUX.
Example
在以下 JavaFX 程序中,我们将通过设置 MAC 的按钮顺序属性来创建一个 ButtonBar。将此代码保存在名为 JavafxButtonBar.java 的文件中。
In the following JavaFX program, we will create a ButtonBar by setting the button order property of MAC. Save this code in a file with the name JavafxButtonBar.java.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class JavafxButtonBar extends Application {
@Override
public void start(Stage stage) {
//Creating required buttons
Button buttonTwo = new Button("Yes");
Button buttonOne = new Button("No");
//Creating a ButtonBar
ButtonBar newButtonbar = new ButtonBar();
// Setting the order of Buttons
newButtonbar.setButtonOrder("BUTTON_ORDER_MAC_OS");
// Adding buttons to the ButtonBar
newButtonbar.getButtons().addAll(buttonOne, buttonTwo);
newButtonbar.setPadding(new Insets(10));
HBox box = new HBox();
box.getChildren().addAll(newButtonbar);
//Setting the stage
Scene scene = new Scene(box, 500, 250);
stage.setTitle("ButtonBar 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 JavafxButtonBar.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxButtonBar
当我们执行上述代码时,它将生成以下输出。
When we execute the above code, it will generate the following output.