从 javafx 中的窗格上的矩形溢出

Overflow from rectangle over pane in javafx

我遇到以下问题: 我有一个矩形,宽度很大。正如您在图片 (1) 中看到的那样,矩形宽度大于窗格宽度。所以矩形的部分应该是不可见的或像图(2)中那样被切割

我不明白你到底想要什么,但我想这会有所帮助

import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class TestApp extends Application {

    @Override
    public void start(Stage stage) throws IOException{
       Rectangle rec = new Rectangle(200 , 50);
       rec.setLayoutX(20);
       rec.setFill(Color.BLUE);
       Pane pane = new Pane();
       pane.setPrefSize(200, 50);
       pane.setStyle(" -fx-background-color : red");
       rec.setWidth(pane.getPrefWidth() - rec.getLayoutX());
       rec.layoutXProperty().addListener((observable, oldValue, newValue) -> {
           rec.setWidth(pane.getWidth() - newValue.doubleValue());
       });
       pane.getChildren().add(rec);
       stage.setScene(new Scene(new Pane(pane) , 400 , 100));
       stage.show();
    }

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

希望如你所愿 如果你关心那些颜色,你可以剪辑矩形 看看这个解释剪辑

或者您案例中的剪辑是

import java.io.IOException;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class TestApp extends Application {

    @Override
    public void start(Stage stage) throws IOException{
       Rectangle rec = new Rectangle(22222 , 50);
       rec.setLayoutX(20);
       rec.setFill(Color.BLUE);
       Pane pane = new Pane();
       pane.setPrefSize(500, 50);
       pane.setStyle(" -fx-background-color : red");
       Rectangle clip = new Rectangle(pane.getPrefWidth(), pane.getPrefHeight());
       clip.setLayoutX(pane.getLayoutX());
       clip.setLayoutX(pane.getLayoutY());
       rec.setClip(clip);
       pane.getChildren().add(rec);
       stage.setScene(new Scene(new Pane(pane) , 400 , 100));
       stage.show();
    }

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