窗格和组有什么区别? [英] What is the difference between a Pane and a Group?
问题描述
在JavaFX中,Pane
和Group
有什么区别?我没什么区别.
In JavaFX, what is the difference between a Pane
and a Group
? I can't make out any difference.
推荐答案
Group
不可调整大小(这意味着它的大小不受场景图中其父级的管理),并且采用了它的子节点. (因此,换句话说,Group
的局部范围将是包含所有子节点范围的最小矩形).如果它大于它在其父级中分配的空间,它将被裁剪.
A Group
is not resizable (meaning that its size is not managed by its parent in the scene graph), and takes on the union of the bounds of its child nodes. (So, in other words, the local bounds of a Group
will be the smallest rectangle containing the bounds of all the child nodes). If it is larger than the space it is allocated in its parent, it will be clipped.
相反,Pane
是可调整大小的,因此其大小由其父级设置,而父级实际上确定了其范围.
By contrast, a Pane
is resizable, so its size is set by its parent, which essentially determine its bounds.
这里是一个快速演示. Group
在顶部,Pane
在下面.两者都在(100,100)
处包含一个固定的蓝色方块,并通过按向左/向右箭头键移动一个绿色方块.请注意,在开始时,蓝色正方形是如何出现在组的左上角的,因为组的局部边界始于其所有子节点的最左上角(即,组的局部边界从
Here is a quick demo. The Group
is on top and the Pane
below. Both contain a fixed blue square at (100,100)
and a green square which is moved by pressing the left/right arrow keys. Note how at the beginning, the blue square appears in the top left corner of the group, because the local bounds of the group start at the top-leftmost point of all its child nodes (i.e. the local bounds of the group extend from (100, 100)
right and down). As you move the green rectangles "off screen", the group adjusts its bounds to incorporate the changes, wherever possible, whereas the pane remains fixed.
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class GroupVsPaneDemo extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Group group = new Group();
VBox.setVgrow(group, Priority.NEVER);
VBox.setVgrow(pane, Priority.NEVER);
VBox vbox = new VBox(group, pane);
Rectangle rect1 = new Rectangle(100, 100, 100, 100);
Rectangle rect2 = new Rectangle(100, 100, 100, 100);
Rectangle rect3 = new Rectangle(200, 200, 100, 100);
Rectangle rect4 = new Rectangle(200, 200, 100, 100);
rect1.setFill(Color.BLUE);
rect2.setFill(Color.BLUE);
rect3.setFill(Color.GREEN);
rect4.setFill(Color.GREEN);
group.getChildren().addAll(rect1, rect3);
pane.getChildren().addAll(rect2, rect4);
Scene scene = new Scene(vbox, 800, 800);
scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
double deltaX ;
switch(e.getCode()) {
case LEFT:
deltaX = -10 ;
break ;
case RIGHT:
deltaX = 10 ;
break ;
default:
deltaX = 0 ;
}
rect3.setX(rect3.getX() + deltaX);
rect4.setX(rect4.getX() + deltaX);
});
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这篇关于窗格和组有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!