假设我们有一个 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();
}
}
这会产生:
取消注释有关剪辑的行会产生:
关于javafx-2 - 如何限制项目的可见性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920680/