围绕一个按钮文本使用偏移 [英] Centering Text on a Button with Offset

查看:168
本文介绍了围绕一个按钮文本使用偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有各种各样的加载与图像的ToggleButtons。按钮大小由图像尺寸determinated和按钮本身是由Java的code创建的。几个按钮的图标有(图标只是加载图像的一部分)上的左侧或右侧。

I have a variety of ToggleButtons that are loaded with an image. The buttons size is determinated by the image size and the button itself is created by JavaCode. A few buttons have icons (the icon is just part of the loaded image) on either the left or right side.

我如何能够通过一定值的文本移动到左边或右边,所以我可以再次但是中央的文字图标偏移?我不介意传递图标作为参数的宽度,但我不能找到任何移动文本进行了一定的。

How can I move the text by a certain value to the left or right so I can center the text again but with the offset of the icon? I don't mind passing the width of the icon as parameter, but I cant find anything to move the text for a certain amount.

按钮从绿色形象创造,右边的图标是它的一部分;总宽度为300,图示正在100;文本应居中,以剩余的200。语言设置原因文本本身不能成为其中的一部分。

The button is created from the green image, the right icon is part of it; total width is 300, icon is taking 100; the text should be centered to the leftover 200. For language setting reason the text itself cant be part of the picture.

推荐答案

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

you can set the style of the button as followed:

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

编辑:结果
您可以使用与HBox:


You can use an 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样式。 ; - )

Everything else is styling with css. ;-)

这篇关于围绕一个按钮文本使用偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆