使用偏移量在按钮上居中文本

时间:2013-04-22 09:29:37

标签: button text javafx-2

我有各种加载了图像的ToggleButtons。按钮大小由图像大小决定,按钮本身由JavaCode创建。一些按钮在左侧或右侧有图标(图标只是加载图像的一部分)。

如何将文本向左或向右移动一定值,以便我可以再次居中文本,但是图标偏移?我不介意将图标的宽度作为参数传递,但是我无法找到任何可以将文本移动一定量的内容。

enter image description here

按钮是从绿色图像创建的,右侧图标是其中的一部分;总宽度为300,图标为100;文本应该以剩下的200为中心。出于语言设置的原因,文本本身不能成为图片的一部分。

1 个答案:

答案 0 :(得分:0)

您可以按照以下方式设置按钮的样式:

// top right bottom left
btn.setStyle("-fx-padding: 5px 5px 5px 5px;");

修改
您可以使用HBox:

HBox hbox = new HBox();
// the text of the "button"
Label lbl_txt = new Label("Text");
// the icon of the "button", i am using Labels for icon(with css)
Label lbl_ico = new Label("ico");
hbox.getChildren().addAll(lbl_txt, lbl_ico);
hbox.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent t) {
        // alternative to Button click
    }
});

其他一切都是用css造型的。 ; - )