javafx - 从javafx中的窗格上的矩形溢出
解决方案
我不明白你到底想要什么,但我想这会有所帮助
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);
}
}
我希望你想要什么如果你关心那些颜色你可以剪辑矩形看看这个解释剪辑 https://stackoverflow.com/a/15922252/14226680
或者你的情况下的剪辑是
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);
}
}