2013-04-10 3 views
7

우리가 AnchorPane을 가지고 있고, 자식이 Pane이고 거기에 우리가 Button 인 것을 상상해보십시오.
Button이 (가) Pane에만 표시되도록하겠습니다.
즉, Pane 내에 완전히 있지 않으면 Pane 가장자리로 잘라야합니다. 이제 ButtonPane 사각형 밖에 있어도 볼 수 있습니다.항목의 가시성을 제한하는 방법은 무엇입니까?

답변

15

이것은 노드의 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

6

당신은 clipping FUNC를 사용할 수 있습니다 이것을 달성하기 위해

public class ClipPane extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 
     Pane clipPane = new Pane(); 
     clipPane.setStyle("-fx-border-color: red;"); 
     clipPane.setPrefSize(200, 200); 

     Rectangle rect = new Rectangle(200, 200); 
     clipPane.setClip(rect); 

     Button btn = new Button("Hello, world!"); 
     btn.relocate(120, 0); 
     clipPane.getChildren().add(btn); 

     AnchorPane root = new AnchorPane(); 
     root.getChildren().add(clipPane); 
     AnchorPane.setTopAnchor(clipPane, 50.); 
     AnchorPane.setLeftAnchor(clipPane, 50.); 

     stage.setScene(new Scene(root, 300, 300)); 
     stage.show(); 
    } 

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

관측 가능한 것을 사용하는 또 다른 접근법. (: 숨겨진 CSS를 oveflow 같은) : 창 경계 외부의 항목을 클립하려면

// create rectangle with sizes of pane, 
// dont need to set x and y explictly 
// as positions of clip node are relative to parent node 
// (to pane in our case) 
Rectangle clipRect = new Rectangle(pane.getWidth(), pane.getHeight()); 

// bind properties so height and width of rect 
// changes according pane's width and height 
clipRect.heightProperty().bind(pane.heightProperty()); 
clipRect.widthProperty().bind(pane.widthProperty()); 

// set rect as clip rect 
pane.setClip(clipRect); 
+0

이,하지만 ... 클립의 처음 크기는 무시하는 큰 보인다. 레이블 테두리를 보존하기 위해이 방법을 수정하는 방법이 있습니까? – Line

관련 문제