我试图在javafx中创建一个自定义的工具栏。该工具栏应该能够在其表面的中心,左侧和右侧(三个部分)显示控件。 问题是,我不知道要实现这一点。我读了很多有关这个问题的提示,但他们不为我工作,或者我做错了什么...如何用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; }
2.It工程为左,右部分,但如何定义中心吗?
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;
}
该方法可以很好地用于布局,但因为这些由右侧部分覆盖我不能监听来自左边和中间部分事件(StackPane的原因) ,所以这个解决方案也没用。
public StackPane createToolBar3() { StackPane toolBar = new StackPane(); Button left = new Button("left button"); Button right = new Button("right button"); Button center = new Button("center button"); HBox leftSection = new HBox(left); leftSection.setAlignment(Pos.CENTER_LEFT); HBox centerSection = new HBox(center); centerSection.setAlignment(Pos.CENTER); HBox rightSection = new HBox(right); rightSection.setAlignment(Pos.CENTER_RIGHT); toolBar.getChildren().addAll(leftSection, centerSection, rightSection); left.setOnAction(event -> System.out.println("left")); right.setOnAction(event -> System.out.println("right")); center.setOnAction(event -> System.out.println("center")); return toolBar; }
上面的方法,我在调用的代码块:
@Override
public void start(Stage stage) {
BorderPane borderPane = new BorderPane();
borderPane.setPrefWidth(500);
borderPane.setPrefHeight(300);
borderPane.setTop(createToolBar4());
stage.setScene(new Scene(borderPane));
stage.show();
}
我会在该问题的任何帮助表示感谢。
看看[这篇文章](http://stackoverflow.com/a/30826914/1759128)。您可以用各自的控件替换帖子中的ImageView,Image和VBox。 – ItachiUchiha