本文介绍了缩放文本框而不截断其中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个简单的页面,其中一段文本以矩形(页面)为界。我希望能够在矩形中放入大量文本,如果文本不适合矩形,那么它会缩小吗?这可能吗?
I'm trying to create a simple "Page" where a piece of text is bounded within a rectangle(the page). I want to be able to put a large amount of text within the rectangle, if the text does not fit within the rectangle then it is scaled down? Is this possible?
谢谢
推荐答案
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 );
}
}
这篇关于缩放文本框而不截断其中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!