我有各种加载了图像的 ToggleButtons。按钮大小由图像大小决定,按钮本身由 JavaCode 创建。一些按钮在左侧或右侧都有图标(图标只是加载图像的一部分)。
如何将文本向左或向右移动某个值,以便我可以再次将文本居中但使用图标的偏移量?我不介意将图标的宽度作为参数传递,但我找不到任何东西可以将文本移动一定量。
该按钮是从绿色图像创建的,右侧图标是其中的一部分;总宽度为 300,图标占用 100;文本应该以剩余的 200 为中心。出于语言设置的原因,文本本身不能成为图片的一部分。
您可以设置按钮的样式如下:
// 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设计的。;-)