Javafx 简明教程
JavaFX - ScrollPane
ScrollPane 是一个控件,提供可滚动的其内容视口。它允许用户使用滚动条垂直或水平滚动内容。它用于显示尺寸较大或其尺寸在屏幕视口有限时可以动态改变的组件。请记住,滚动条的尺寸取决于组件的尺寸。下图显示了一个带有垂直滚动条的可滚动视口 −
ScrollPane is a control that provides a scrollable viewport of its contents. It allows the user to scroll the content vertically or horizontally by using scroll bars. It is used to display a component that is large or one whose size can change dynamically when the screen viewport is limited. Remember, the size of the scroll bars depends on the size of the component. The below figure shows a scrollable viewport with vertical scroll bars −
ScrollPane in JavaFX
在 JavaFX 中,滚动窗格控件由名为 ScrollPane 的类表示。此类属于包 javafx.scene.control 。通过实例化此类,我们可以在JavaFX中创建 ScrollPane 控件。此类具有以下构造函数 −
In JavaFX, the scrollpane control is represented by a class named ScrollPane. This class belongs to the package javafx.scene.control. By instantiating this class, we can create a ScrollPane control in JavaFX. This class has the following constructors −
-
ScrollPane() − It constructs a ScrollPane without any node.
-
ScrollPane(Node content) − It constructs a new ScrollPane with the specified node.
Steps to create a ScrollPane in JavaFX
要创建 JavaFX ScrollPane,请遵循以下步骤。
To create a ScrollPane in JavaFX, follow the steps given below.
Step 1: Create a node to display within the ScrollPane
在 JavaFX 中,滚动窗格可以显示包含图像、文本或图表。因此,实例化尊敬的类以创建所需的节点。在此,我们使用图像作为 ScrollPane 的内容 −
In JavaFX, the scrollpane can display nodes that can contain an image, a text or a chart. Hence, instantiate the respected class to create the desired node. Here, we are using an image as a content for the ScrollPane −
// create an image view
ImageView imageTp = new ImageView(new Image("tutorials_point.jpg"));
Step 2: Instantiate the ScrollPane class
在 start() 方法中实例化名为 ScrollPane 的类。此操作将为 ImageView 创建一个 ScrollPane。
Instantiate the class named ScrollPane inside the start() method. This action will create a ScrollPane for the ImageView.
// create a scroll pane
ScrollPane newscroll = new ScrollPane();
Step 3: Set the Content of the ScrollPane
要设置 ScrollPane 的内容,我们需要使用名为 setContent() 的方法。将 ImageView 对象作为参数值传递给此方法。
To set the content of the ScrollPane, we use the method named setContent(). Pass the ImageView object to this method as a parameter value.
// set the content of the scroll pane
newscroll.setContent(imageTp);
Step 4: Launch the Application
一旦创建 ScrollPane 并设置其内容,就请按照以下所给步骤正确启动应用程序 −
Once the ScrollPane is created and its content is set, follow the given steps below to launch the application properly −
-
Firstly, instantiate the class named Scene by passing the ScrollPane object as a parameter value to its constructor along with the dimensions of the application screen.
-
Then, set the title of 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 程序演示了如何在 JavaFX 应用程序中创建 ScrollPane。将此代码保存到名为 JavafxScrollpane.java 的文件中。
The following JavaFX program demonstrates how to create a ScrollPane within JavaFX application. Save this code in a file with the name JavafxScrollpane.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class JavafxScrollpane extends Application {
@Override
public void start(Stage stage) {
// creating an image view
ImageView imageTp = new ImageView(new Image("tutorials_point.jpg"));
// creating a scroll pane
ScrollPane newscroll = new ScrollPane();
// setting the content of the scroll pane
newscroll.setContent(imageTp);
// creating a scene and stage
Scene scene = new Scene(newscroll, 500, 300);
stage.setTitle("ScrollPane in JavaFX");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
要从命令提示符编译并执行已保存的 Java 文件,请使用以下命令−
To compile and execute the saved Java file from the command prompt, use the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane
当我们执行上述代码时,它将生成以下输出。
When we execute the above code, it will generate the following output.
Disable Vertical Scroll bar of ScrollPane in JavaFX
ScrollPane 类提供了两种方法 setHbarPolicy() 和 setVbarPolicy() ,用于指定何时使用滚动条。要启用滚动条,我们需要将 ScrollBarPolicy.ALWAYS 属性传递到各自的方法,要禁用,我们使用 ScrollBarPolicy.NEVER 属性。
The ScrollPane class provides two methods namely setHbarPolicy() and setVbarPolicy() to specify when to use the scroll bars. To enable the scroll bar, we pass the ScrollBarPolicy.ALWAYS property to the respective methods and to disable, we use ScrollBarPolicy.NEVER property.
Example
在以下示例中,我们将禁用 ScrollPane 的垂直滚动条。将此代码保存到名为 JavafxScrollpane.java 的文件中。
In the following example, we are going to disable the vertical scroll bar of the ScrollPane. Save this code in a file with the name JavafxScrollpane.java.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class JavafxScrollpane extends Application {
@Override
public void start(Stage stage) {
// creating an image view
ImageView imageTp = new ImageView(new Image("tutorials_point.jpg"));
// creating a scroll pane
ScrollPane newscroll = new ScrollPane();
// disbaling the vertical scroll bar
newscroll.setHbarPolicy(ScrollBarPolicy.ALWAYS);
newscroll.setVbarPolicy(ScrollBarPolicy.NEVER);
// setting the content of the scroll pane
newscroll.setContent(imageTp);
// creating a scene and stage
Scene scene = new Scene(newscroll, 500, 300);
stage.setTitle("ScrollPane 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 by using the following commands −
javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxScrollpane
在执行上述代码时,它将生成以下输出。
On executing the above code, it will generate the following output.