Javafx 2 如何将文本放入圆形对象中以从圆形显示它';s中心?
我很好奇有没有办法将文本(我通常会使用动态变化的数字)放入圆对象,或者创建文本对象并将其边界设置为圆的中心是显示它的唯一方式?我将感谢您的每一个回复:)将圆圈和文本放在堆栈窗格中,并将文本边界类型计算设置为可视:Javafx 2 如何将文本放入圆形对象中以从圆形显示它';s中心?,javafx-2,javafx,Javafx 2,Javafx,我很好奇有没有办法将文本(我通常会使用动态变化的数字)放入圆对象,或者创建文本对象并将其边界设置为圆的中心是显示它的唯一方式?我将感谢您的每一个回复:)将圆圈和文本放在堆栈窗格中,并将文本边界类型计算设置为可视: Circle circle = new Circle(); Text text = new Text("42"); text.setBoundsType(TextBoundsType.VISUAL); StackPane stack = new StackPane(); stack.
Circle circle = new Circle();
Text text = new Text("42");
text.setBoundsType(TextBoundsType.VISUAL);
StackPane stack = new StackPane();
stack.getChildren().add(circle, text);
您可以使用时间线更改圆圈中文本的值
下面是一个完整的示例:
import javafx.animation.*;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.*;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TextInCircle extends Application {
public static void main(String[] args) throws Exception { launch(args); }
private static final int R = 150;
private static final Color lineColor = Color.FIREBRICK.deriveColor(0, 1, 1, .6);
@Override
public void start(final Stage stage) throws Exception {
final Circle circle = createCircle();
final Text text = createText();
final Line l1 = createLine(lineColor, 0, R - 0.5, 2 * R, R - 0.5);
final Line l2 = createLine(lineColor, R - 0.5, 0, R - 0.5, 2 * R);
// Group group = new Group(circle, text, l1 , l2);
Group group = new Group(circle, l1 , l2);
StackPane stack = new StackPane();
stack.getChildren().addAll(group, text);
stage.setScene(new Scene(stack));
stage.show();
animateText(text);
}
private Circle createCircle() {
final Circle circle = new Circle(R);
circle.setStroke(Color.FORESTGREEN);
circle.setStrokeWidth(10);
circle.setStrokeType(StrokeType.INSIDE);
circle.setFill(Color.AZURE);
circle.relocate(0, 0);
return circle;
}
private Line createLine(Color lineColor, double x1, double y1, double x2, double y2) {
Line l1 = new Line(x1, y1, x2, y2);
l1.setStroke(lineColor);
l1.setStrokeWidth(1);
return l1;
}
private Text createText() {
final Text text = new Text("A");
text.setFont(new Font(30));
text.setBoundsType(TextBoundsType.VISUAL);
// centerText(text);
return text;
}
private void centerText(Text text) {
double W = text.getBoundsInLocal().getWidth();
double H = text.getBoundsInLocal().getHeight();
text.relocate(R - W / 2, R - H / 2);
}
private void animateText(final Text text) {
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(1), new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
char newValue = (char) ((text.getText().toCharArray()[0] + 1) % 123);
if (newValue == 0) newValue = 'A';
text.setText("" + newValue);
// centerText(text);
}
}));
timeline.setCycleCount(1000);
timeline.play();
}
}
导入javafx.animation.*;
导入javafx.application.application;
导入javafx.event.*;
导入javafx.scene.*;
导入javafx.scene.layout.StackPane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.*;
导入javafx.scene.text.*;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类TextInCircle扩展了应用程序{
公共静态void main(字符串[]args)引发异常{launch(args);}
专用静态最终整数R=150;
私有静态最终颜色lineColor=Color.FIREBRICK.deriveColor(0,1,1,6);
@凌驾
public void start(final Stage)引发异常{
最终圆=创建圆();
最终文本文本=createText();
最后一行l1=createLine(lineColor,0,R-0.5,2*R,R-0.5);
最终线条l2=创建线条(线条颜色,R-0.5,0,R-0.5,2*R);
//组=新组(圆圈、文本、l1、l2);
组=新组(圆圈、l1、l2);
StackPane stack=新的StackPane();
stack.getChildren().addAll(组,文本);
舞台场景(新场景(堆栈));
stage.show();
animateText(文本);
}
private Circle createCircle(){
最终圆=新圆(R);
圆整定行程(颜色为绿色);
圆。设定行程宽度(10);
圆形设置行程类型(行程类型内部);
圆形。setFill(颜色为蓝色);
重新定位(0,0);
返回圈;
}
专用线createLine(彩色线颜色,双x1,双y1,双x2,双y2){
l1线=新线(x1、y1、x2、y2);
l1.设定行程(线条颜色);
l1.设定行程宽度(1);
返回l1;
}
私有文本createText(){
最终文本=新文本(“A”);
text.setFont(新字体(30));
text.setBoundsType(textbundstype.VISUAL);
//中心文本(文本);
返回文本;
}
专用void中心文本(文本){
double W=text.getBoundsInLocal().getWidth();
双H=text.getBoundsInLocal().getHeight();
重新定位(R-W/2,R-H/2);
}
私有void animateText(最终文本){
Timeline Timeline=新的时间线(新的关键帧(持续时间.秒(1),新的EventHandler()){
@重写公共无效句柄(ActionEvent ActionEvent){
char newValue=(char)((text.getText().toCharArray()[0]+1)%123);
如果(newValue==0)newValue='A';
text.setText(“+newValue”);
//中心文本(文本);
}
}));
时间线。setCycleCount(1000);
timeline.play();
}
}
如果您愿意手动放置文本,而不是使用StackPane,注释掉的代码还包括手动放置文本的代码。这正是我想要的!。另一方面,我有42个圆,所以我必须创建42个堆栈窗格和文本对象,对吗?。这看起来像一个愚蠢的问题,但我问的是因为我不想处理凌乱的代码:)。谢谢你的回答。好吧,我想我解决了我的最后一个问题。我将使用SceneBuilder手动将42个堆叠窗格放置在指定位置。我遇到了文本在圆圈内正确居中的问题。使用
TextBoundsType.LOGICAL\u VERTICAL\u CENTER
为我完成了这个任务。@TrackerSB这是一个很好的替代解决方案。