1

我正在尝试创建一个简单的“页面”,其中一段文本以矩形(页面)为界。我希望能够在矩形内放置大量文本,如果文本不适合矩形,那么它会按比例缩小吗?这可能吗?

谢谢

4

1 回答 1

3
import javafx.application.Application;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Test extends Application {

    @Override
    public void start( Stage stage ) throws Exception {
        double width = 100;
        // create rectangle of specified width
        Rectangle r = new Rectangle( 90, 200, width, 20 );
        // get bounds of the rectangle
        Bounds rb = r.getBoundsInLocal();
        // create two identical texts for comparison
        Text t1 = new Text(50, 170, "Some long text that would not fit.");
        Text t2 = new Text(50, 190, "Some long text that would *** fit.");
        // get bounds of the text
        // this will return how much space the text actually takes
        Bounds t2b = t2.getBoundsInLocal();
        // find the scale down amount
        double scalex = rb.getWidth()/t2b.getWidth();
        // scale the text down to what the rect is
        t2.setScaleX( scalex );

        Pane root = new Pane();
        root.getChildren().addAll( r, t1, t2 );

        stage.setScene( new Scene( root, 800, 600 ) );
        stage.show();
    }

    public static void main( String args[] ) {
        launch( args );
    }
}
于 2012-11-09T01:07:35.707 回答