JavaFX 区域与窗格
Region 和 Pane 用于将可调整大小的子节点调整到它们的首选大小,而不是重新定位它们。本教程演示了 JavaFX 中 Region
和 Pane
之间的区别。
JavaFX 区域
JavaFX Region
类可用作所有 JavaFX 布局窗格的基类。它提供了一些用于扩展区域的所有 JavaFX 布局类共享的属性。
JavaFX 有许多类是 Pane
、Control
、Chart
和 Axis
的子类。这四个类和所有其他类是 Region
类的子类;它们都将具有与 Region
相同的属性。
区域属性如下:
Background
Content Area
Padding
Borders
Margin
Region Insets
JavaFX 窗格
JavaFX Pane
是 Region
的子类。Pane
是一个布局容器,可以包含许多其他 JavaFX 组件来进行布局。
它不提供任何布局算法,但在位置上显示它包含的组件,这对于组件来说是可取的。Pane
使用其子组件指定的 layoutX
和 layoutY
来确定它们要显示的位置。
Pane
继承了 JavaFX Region
类的所有属性,因为这是一个子类。所以背景、内容区域、填充、边框等也可以用于 Pane
。
JavaFX 区域与窗格
Region
和 Pane
都用于将可调整大小的子节点调整到其首选大小。他们永远不会重新定位它们。
-
Region
是具有子节点的超类。Pane
是Region
类的子类,带有子节点。 -
Region
不允许通过公共 API 操作子节点;另一方面,Pane
将让公共 API 操作子节点。 -
Region.getChildren()
是受保护的,而Pane.getChildren
不是受保护的方法。 -
Region
专用于组件开发人员;这就是为什么它可以选择允许或不允许 API 用户操纵子系统,如Pane
、Hbox
等。另一方面,Pane
不提供任何类似的权限; API 用户可以直接操作子节点。
让我们尝试一下 Region
和 Pane
的示例。
区域
的示例:
package delftstack;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavaFX_Reigon extends Application {
public void start(Stage Label_Stage) {
String Label_Text = "DelftStack is a resource for everyone interested in programming.";
//Create a Label
Label Demo_Label = new Label(Label_Text);
//wrap the label
Demo_Label.setWrapText(true);
//Set the maximum width of the label
Demo_Label.setMaxWidth(300);
//Set the position of the label
Demo_Label.setTranslateX(30);
Demo_Label.setTranslateY(30);
//Create a Region
Region Label_Root = new Region();
// Add Children to region which will throw an error
Label_Root.getChildren().add(Demo_Label);
//Set the stage
Scene Label_Scene = new Scene(Label_Root, 595, 150, Color.BEIGE);
Label_Stage.setTitle("Region Example");
Label_Stage.setScene(Label_Scene);
Label_Stage.show();
}
public static void main(String args[]){
launch(args);
}
}
上面的代码用于将文本包装在标签中。如我们所见,我们将子节点添加到 Region
,这是不可能的,所以这段代码应该会抛出错误。
见输出:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:465)
at javafx.graphics@18.0.1/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:364)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1071)
让我们为 Pane
尝试相同的示例,我们可以将子节点添加到窗格中。参见示例:
package delftstack;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class JavaFX_Reigon extends Application {
public void start(Stage Label_Stage) {
String Label_Text = "DelftStack is a resource for everyone interested in programming.";
//Create a Label
Label Demo_Label = new Label(Label_Text);
//wrap the label
Demo_Label.setWrapText(true);
//Set the maximum width of the label
Demo_Label.setMaxWidth(300);
//Set the position of the label
Demo_Label.setTranslateX(30);
Demo_Label.setTranslateY(30);
//Create a Pane
Pane Label_Root = new Pane();
// Add Children to Pane which will work properly
Label_Root.getChildren().add(Demo_Label);
//Set the stage
Scene Label_Scene = new Scene(Label_Root, 595, 150, Color.BEIGE);
Label_Stage.setTitle("Pane Example");
Label_Stage.setScene(Label_Scene);
Label_Stage.show();
}
public static void main(String args[]){
launch(args);
}
}
这段代码可以正常工作,因为我们可以将子节点添加到窗格中。见输出:
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。