java JavaFX:在 ButtonBar 内对齐按钮(使用 SceneBuilder 或 fxml)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32426848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:11:00  来源:igfitidea点击:

JavaFX: Align Buttons inside ButtonBar (using SceneBuilder or fxml)

javajavafxjavafx-8fxmlscenebuilder

提问by drkthng

I have a JavaFX ButtonBarwith two Buttons (created via SceneBuilder).

I want one of the buttons to be left-aligned and the other right-aligned. (see screenshot)

我有一个ButtonBar带有两个按钮的 JavaFX (通过 SceneBuilder 创建)。

我希望其中一个按钮左对齐,另一个右对齐。(见截图)

enter image description here

在此处输入图片说明

From the docs I already know how I could achieve this inside the java-source-code:

从文档中,我已经知道如何在 java-source-code 中实现这一点:

ButtonBar.setButtonData(newButton, ButtonData.LEFT);

BUT

I want to know how to achieve this WITHOUT having to write this inside my java-files but
how I can achieve this using just SceneBuilder or the corresponding fxml file.

我想知道如何在不必在我的 java 文件中编写它的情况
下实现这一点,但是我如何仅使用 SceneBuilder 或相应的 fxml 文件来实现这一点

My .fxml file currently looks like this:

我的 .fxml 文件目前看起来像这样:

<ButtonBar>
  <buttons>
    <Button text="New" />
    <Button text="Save" />
  </buttons>
</ButtonBar>

* I'm on Windows
** This answeris not what I want, because he is using a ToolBar, but I want to know how to do this with a ButtonBar (and his approach does not work for the ButtonBar)

* 我在 Windows 上
**这个答案不是我想要的,因为他使用的是 ToolBar,但我想知道如何使用 ButtonBar(他的方法不适用于 ButtonBar)

回答by drkthng

After some Trial and Error I found at least a way to do it directly via the .fxml-file:

经过一些试验和错误后,我至少找到了一种直接通过 .fxml 文件执行此操作的方法:

You can assign the Button elements with ButtonBar.buttonDataattributes and then assign a value to them.

您可以为 Button 元素分配ButtonBar.buttonData属性,然后为它们分配一个值。

<ButtonBar>
  <buttons>
    <Button text="New" ButtonBar.buttonData="LEFT" />
    <Button text="Save" ButtonBar.buttonData="RIGHT" />
  </buttons>
</ButtonBar>

The docs for the ButtonBar.ButtonData enumare pretty straighforward. So I found the solution to my problem with the "LEFT" and "RIGHT" enum values.

ButtonBar.ButtonData 枚举的文档非常简单。所以我用“LEFT”和“RIGHT”枚举值找到了我的问题的解决方案。

STILL

仍然

If anyone knows/finds out how to do this directly in SceneBuilder, I would be grateful!

如果有人知道/了解如何直接在 SceneBuilder 中执行此操作,我将不胜感激!

回答by Nyein

There is the "Transforms" property in Layouttab of the button on the right side of Scene Builder. Change "Translate X". (The position of the button will change horizontally.) "Translate Y" will change the button's position vertically. You need to calculate the position first.

在Scene Builder 右侧按钮的Layout选项卡中有“ Transforms”属性。更改“翻译X”。(按钮的位置会水平改变。)“平移 Y”将改变按钮的垂直位置。您需要先计算位置。

I just found it.

我刚找到它。

回答by OtenMoten

enter image description here

在此处输入图片说明

Via the Inspector, usually on the right side in NetBeans, you will find the tab "Layout". Open it and scroll to the section "Transform".

通过检查器,通常在 NetBeans 的右侧,您将找到选项卡“布局”。打开它并滚动到“转换”部分。

The option "Translate X" is for adjusting the buttons in the buttonbar at the x-axis.

“平移 X”选项用于调整 x 轴按钮栏中的按钮。