2013-04-10 12 views
7

AnchorPaneがあり、子供がPaneであり、そこにはButtonがあるとします。
このはこのPaneの内部にのみ表示します。
Paneに完全に含まれていない場合は、Paneのエッジでカットする必要があります。 Buttonは、Paneの長方形から外れていても表示されます。アイテムの表示を制限する方法は?

答えて

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

は生成します。

あなたは clipping FUNCを使用することができます

with clip

6

これを達成するために

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:hiddenのような)ペイント範囲外のアイテムをクリップするには:

+0

これは素晴らしいですが...クリップの初期サイズは無視されるようです。ラベルの枠線を維持するためにこれを変更する方法はありますか? – Line

関連する問題