gpt4 book ai didi

javafx-2 - 如何限制项目的可见性?

转载 作者:行者123 更新时间:2023-12-02 19:32:18 27 4
gpt4 key购买 nike

假设我们有一个 AnchorPane,它有子 Pane,并且我们有 Button,例如。
我希望此 Button 仅显示在此 Pane 内。
换句话说,如果它不完全在 Pane 内,它就会被 Pane 边缘切割。现在,即使 Button 位于 Pane 矩形之外,它仍然可见。

最佳答案

这就是clip节点的创建。

示例:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class ClipTest extends Application {

public static void main(String[] args) {
launch(args);
}

@Override
public void start(Stage primaryStage) throws Exception {

Group root = new Group();

StackPane pane = new StackPane();

pane.setMaxWidth(100);
pane.setMaxHeight(100);
pane.setLayoutX(50);
pane.setLayoutY(50);


Rectangle rect = new Rectangle(100, 100);

rect.setFill(null);
rect.setStroke(Color.RED);

Rectangle rect2 = new Rectangle(150, 150);

rect2.setFill(Color.BLUE);

pane.getChildren().addAll(rect2, rect);

root.getChildren().add(pane);


// Rectangle clip = new Rectangle(100, 100);
// clip.setLayoutX(25);
// clip.setLayoutY(25);
// pane.setClip(clip);

Scene scene = new Scene(root, 250, 250);

primaryStage.setScene(scene);
primaryStage.show();
}
}

这会产生:

without clip

取消注释有关剪辑的行会产生:

with clip

关于javafx-2 - 如何限制项目的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920680/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com