首页 > 解决方案 > 从javafx中的窗格上的矩形溢出

问题描述

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

图片编号

标签: 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);
    }
}

推荐阅读