本文介绍了如何在javafx的中心锚定文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是代码:
package je3.thread;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/**
* Text seemed to be anchored at the bottom left corner.
* See screenshot here: http://i60.tinypic.com/2mnnmrn.jpg
*/
public class ShowText extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
pane.setPadding(new Insets(0, 0, 0, 0));
Text text1 = new Text(20, 20, "Programming fun");
// text1.setFont(Font.font("Courier", BOLD, FontPosture.ITALIC, 15));
Text text2 = new Text(30, 30, "Programming fun");
Text text3 = new Text(40, 40, "Programming fun");
// text3.setFill(Color.RED);
// text3.setUnderline(true);
pane.getChildren().addAll(text1, text2,text3);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
结果:
我正在寻找的方法是指定一个坐标并将给定文本对象的中心锚定到该坐标。
What I am looking for is a method to specify a coordinate and anchor the center of a given text object to that coordinate.
推荐答案
这样的话:
private Text centerTextOnCoordinate( String text, double x, double y )
{
Text txtShape = new Text( x, y, text );
txtShape.setX( txtShape.getX() - txtShape.getLayoutBounds().getWidth() / 2 );
return txtShape;
}
这篇关于如何在javafx的中心锚定文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!