将标签绑定到场景底部中心-JavaFX
我试图找出如何在场景底部完美地居中和绑定标签。我在这里有一个简单的测试应用程序来显示我在使用什么以及我的问题是什么将标签绑定到场景底部中心-JavaFX,java,javafx,position,Java,Javafx,Position,我试图找出如何在场景底部完美地居中和绑定标签。我在这里有一个简单的测试应用程序来显示我在使用什么以及我的问题是什么 import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.Pane; import javafx.stage.Stage; public class LabelTest extends
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class LabelTest extends Application {
@Override
public void start(Stage stage) throws Exception {
Pane root = new Pane();
Scene scene = new Scene(root, 400, 400);
Label label = new Label("Testing testing 1 2 3");
label.layoutXProperty().bind(scene.widthProperty().divide(2).subtract(label.getWidth() / 2)); //Should align label to horizontal center, but it is off
label.layoutYProperty().bind(scene.heightProperty().subtract(label.getHeight() + 35)); //Aligns the label to bottom of scene
root.getChildren().add(label);
stage.setScene(scene);
stage.show();
}
}
我的定位背后的逻辑对我来说是有意义的,所以我不确定为什么它不是水平居中的。我在下面提供了一个屏幕截图,显示了输出的样子:
下面是我想要它看起来的更多内容(仍然有点偏离,但你明白了)
提前感谢所有帮助我的人 问题是您在绑定时使用的是宽度/高度值。在本例中,它将为0,因为它们尚未渲染。您还需要为计算绑定这些属性
label.layoutXProperty().bind(scene.widthProperty().divide(2).subtract(label.widthProperty().divide(2)));
label.layoutYProperty().bind(scene.heightProperty().subtract(label.heightProperty().add(35)));
问题是您在绑定时使用的是宽度/高度值。在本例中,它将为0,因为它们尚未渲染。您还需要为计算绑定这些属性
label.layoutXProperty().bind(scene.widthProperty().divide(2).subtract(label.widthProperty().divide(2)));
label.layoutYProperty().bind(scene.heightProperty().subtract(label.heightProperty().add(35)));
让布局管理器为您进行布局:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LabelTest extends Application {
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("Testing testing 1 2 3");
BorderPane root = new BorderPane();
//center label by
//BorderPane.setAlignment(label, Pos.CENTER);
//root.setBottom(label);
//OR
root.setBottom(new StackPane(label));
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
让布局管理器为您进行布局:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class LabelTest extends Application {
@Override
public void start(Stage stage) throws Exception {
Label label = new Label("Testing testing 1 2 3");
BorderPane root = new BorderPane();
//center label by
//BorderPane.setAlignment(label, Pos.CENTER);
//root.setBottom(label);
//OR
root.setBottom(new StackPane(label));
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
这样可以避免使用绑定和尝试偏移标签的中心。谢谢大家!@Brent注意到,
StackPane
并非绝对必要。您还可以直接将底部设置为标签
,然后调用BorderPane.setAlignment(标签,位置中心)
。这并不是说两种方法都比另一种好。@Slaw谢谢你的评论。我在答案中添加了这个选项。这避免了使用绑定和试图偏移标签的中心。谢谢大家!@Brent注意到,StackPane
并非绝对必要。您还可以直接将底部设置为标签
,然后调用BorderPane.setAlignment(标签,位置中心)
。这并不是说两种方法都比另一种好。@Slaw谢谢你的评论。我在回答中添加了这个选项。