我正在尝试在javafx中创建自定义工具栏.此工具栏应该能够在其表面的中心,左侧和右侧(三个部分)显示控件.问题是我不知道这个.我读了很多与这个问题相关的提示,但是他们不适合我,或者我做错了什么......
无论如何,我写了几个方法,它们代表了实现我的工具栏的不同方法,但没有一个正常工作.在这里你有我的尝试:
使用HBox Hgrow属性作为弹簧.没工作.
public ToolBar createToolBar()
{
ToolBar toolBar = new ToolBar();
Pane emptyPane = new Pane();
HBox spring = new HBox(emptyPane);
spring.setHgrow(emptyPane, Priority.ALWAYS);
toolBar.getItems().addAll(spring, new Label("LABEL"));
return toolBar;
}
Run Code Online (Sandbox Code Playgroud)2.它适用于左右两部分,但如何定义中心部分?
public AnchorPane createToolBar2()
{
AnchorPane toolBar = new AnchorPane();
Label leftLabel = new Label("left");
Label rightLabel = new Label("right");
toolBar.getChildren().addAll(leftLabel, rightLabel);
toolBar.setLeftAnchor(leftLabel, 0.0);
toolBar.setRightAnchor(rightLabel, 0.0);
return toolBar;
}
Run Code Online (Sandbox Code Playgroud)
这种方法适用于布局,但我无法监听左侧和中间部分的事件,因为这些事件由右侧部分覆盖(StackPane是原因),因此该解决方案也没用.
public StackPane createToolBar3()
{
StackPane toolBar = new StackPane();
Button left = new Button("left button");
Button right = new …Run Code Online (Sandbox Code Playgroud)如何使此窗口上的"右"(第二个)按钮右对齐?
class HelloApp extends Application {
override def start(primaryStage: Stage): Unit = {
val root = new FlowPane(Orientation.VERTICAL) {
setPadding(new Insets(10, 10, 10, 10))
setVgap(10)
getChildren.add(new Button("Left"))
getChildren.add(new Button("Right") { setAlignment(Pos.CENTER_RIGHT) }) // this doesn't seem to be working
}
primaryStage.setScene(new Scene(root, 400, 400))
primaryStage.show()
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢