Javafx 简明教程
JavaFX - Accordion
一个 accordion 用作一个或多个标题面板的容器。这些标题面板是一些带有标题的面板,可以通过单击其标题来展开或收缩它们。但是,一次只允许打开一个标题窗格。手风琴对于将用户界面组织成可以根据需要进行隐藏或显示的部分非常有用。
An accordion serves as a container for one or more title panels. These title panels are a panel with titles and they can be expanded or collapsed by clicking on their headers. However, only one title pane is allowed to open at a time. An accordion is useful for organizing the UI into sections that can be hidden or shown as needed.
下图显示了一个手风琴−
The below figure depicts an Accordion −
Accordion in JavaFX
在 JavaFX 中,手风琴组件由名为 Accordion 的类表示。此类属于 javafx.scene.control 包。通过实例化此类,我们可以在 JavaFX 中创建一个手风琴节点。
In JavaFX, an accordion is represented by a class named Accordion. This class belongs to the package javafx.scene.control. By instantiating this class, we can create an Accordion node in JavaFX.
此类有两个可用的构造函数,如下所示:
Two constructors are available for this class, and they are as follows −
-
Accordion() − It is used for creating an accordion without TitledPane.
-
Accordion(…TitledPanes) − It will create an accordion with the specified TitledPane.
Steps to create an Accordion in JavaFX
按照下列步骤,在 JavaFX 中创建手风琴。
Follow the steps given below to create an accordion in JavaFX.
Step 1: Create two or more TitledPane
我们可以通过实例化名为 TitledPane 的类(它属于一个包* javafx.scene.control*)在 JavaFX 中创建标题面板。按如下所示实例化此类:
We can create title panes in JavaFX by instantiating the class named TitledPane which belongs to a package * javafx.scene.control*. Instantiate this class as shown below −
//Creating a TitledPane object
TitledPane paneOne = new TitledPane();
同样,创建所需数量的标题面板。
Similarly, create the required number of title panes.
Step 2: Set title and contents to the TitledPane
使用 setText() 和 setContent() 方法分别指定标题面板的标题和内容,如下面的代码块所示。
Specify the title and content of the title pane using the setText() and setContent() methods respectively as shown in the following code block.
paneOne.setText("Your Name");
paneOne.setContent(new Label("My name is: \n Ansh"));
记住,我们可以根据我们的要求使用 JavaFX 的不同 UI 节点,自定义 TitledPane 的内容。
Remember, we can customize the content of the TitledPane as per our requirements with different UI nodes of JavaFX.
Step 3: Instantiate Accordion class
实例化 Accordion 类的包 javafx.scene.control ,而不向其构造函数传递任何参数值,并使用以下代码块添加所有 TitledPane:
Instantiate the Accordion class of package javafx.scene.control without passing any parameter value to its constructor and add all the TitledPane using the following code blocks −
Accordion accordionNew = new Accordion();
accordionNew.getPanes().addAll(paneOne, paneTwo, paneThree);
Step 4: Launching Application
创建手风琴后,按照下面给定的步骤正确启动应用程序:
Once the accordion is created, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named VBox by passing the Accordion object as a parameter value to its constructor.
-
Then, instantiate the class named Scene by passing the VBox 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 创建手风琴的程序。将此代码保存在一个名为 JavafxAccordion.java 的文件中。
Following is the program which will create an accordion using JavaFX. Save this code in a file with the name JavafxAccordion.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.TitledPane;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavafxAccordion extends Application {
@Override
public void start(Stage stage) {
//Creating the first TitledPane
TitledPane paneOne = new TitledPane();
paneOne.setText("Your Name");
paneOne.setContent(new Label("My name is: \n Ansh"));
//Creating the second TitledPane
TitledPane paneTwo = new TitledPane();
paneTwo.setText("Your Address");
paneTwo.setContent(new Label("My address is: \n Hyderabad \n Telangana"));
//Creating the third TitledPane
TitledPane paneThree = new TitledPane();
paneThree.setText("Your Job");
paneThree.setContent(new Label("My job role is: \n Content Engineer"));
//Creating an Accordion for all TitledPane
Accordion accordionNew = new Accordion();
accordionNew.getPanes().addAll(paneOne, paneTwo, paneThree);
accordionNew.setExpandedPane(paneOne);
VBox root = new VBox(accordionNew);
//Setting the stage
Scene scene = new Scene(root, 500, 500, Color.BEIGE);
stage.setTitle("Accordion 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 JavafxAccordion.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxAccordion
在执行时,上述程序会生成一个 JavaFX 窗口,显示一个手风琴,如下所示。
On executing, the above program generates a JavaFX window displaying an accordion as shown below.
Creating an Accordion in JavaFX using its parameterized constructor
如前所述,在 JavaFX 中创建手风琴有两种方法:一种利用手风琴类的 default constructor ,而另一种使用其 parameterized constructor 。在下一个示例中,我们将在 JavaFX 中使用手风琴类的参数化构造函数创建手风琴。将此 Java 代码保存在一个名为 JavafxAccordion.java 的文件中。
As previously discussed, there are two ways to create an Accordion in JavaFX: one utilizes the default constructor of Accordion class, while the other uses its parameterized constructor. In the next example, we are going to use the parameterized constructor of the Accordion class to create an accordion in JavaFX. Save this Java code in a file with the name JavafxAccordion.java.
Example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Accordion;
import javafx.scene.control.TitledPane;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavafxAccordion extends Application {
@Override
public void start(Stage stage) {
//Creating the first TitledPane
TitledPane paneOne = new TitledPane();
paneOne.setText("Your Name");
paneOne.setContent(new Label("My name is: \n Ansh"));
//Creating the second TitledPane
TitledPane paneTwo = new TitledPane();
paneTwo.setText("Your Address");
paneTwo.setContent(new Label("My address is: \n Hyderabad \n Telangana"));
//Creating the third TitledPane
TitledPane paneThree = new TitledPane();
paneThree.setText("Your Job");
paneThree.setContent(new Label("My job role is: \n Content Engineer"));
//Creating an Accordion for all TitledPane
Accordion accordionNew = new Accordion(paneOne, paneTwo, paneThree);
accordionNew.setExpandedPane(paneThree);
VBox root = new VBox(accordionNew);
//Setting the stage
Scene scene = new Scene(root, 500, 500, Color.BEIGE);
stage.setTitle("Accordion 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 JavafxAccordion.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxAccordion
当我们执行上述代码时,它将生成以下输出。
When we execute the above code, it will generate the following output.