【发布时间】:2021-12-20 16:40:11
【问题描述】:
【问题讨论】:
-
minimal reproducible example 请 .. 说:使用剪辑(或为您进行剪辑的布局)
标签: 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);
}
}
【讨论】: