2015-06-14 123 views
4

我试图在javafx中创建一个自定义的工具栏。该工具栏应该能够在其表面的中心,左侧和右侧(三个部分)显示控件。 问题是,我不知道要实现这一点。我读了很多有关这个问题的提示,但他们不为我工作,或者我做错了什么...如何用javaFX中的左边,中间和右边部分创建工具栏?

无论如何,我写了几种方法,它们代表不同的方式来实现我的工具栏,但没有正常工作。 在这里你有我的尝试:

  1. 使用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(); 
        } 
    

    我会在该问题的任何帮助表示感谢。

    +0

    看看[这篇文章](http://stackoverflow.com/a/30826914/1759128)。您可以用各自的控件替换帖子中的ImageView,Image和VBox。 – ItachiUchiha

    回答

    3

    只需使用一个HBox中,而不是StackPane。 StackPane将节点放在彼此的顶部。

    See a modified example of your third attempt.

    另一种选择是使用FX工具栏中,类似于如何已经提到jewelsea

    See an example using a ToolBar.

    尝试都应该是足够灵活的满足您的需求。它们的差异在于您对布局的控制程度以及您可以从标准ToolBar继承的功能数量。

    +0

    下面看到我的回答感谢您的有用的例子!我试图扩展FX工具栏的方法,它完美的作品:) – Qbisiek

    5

    ToolBar截面对齐的弹簧方法对我来说很好。

    springtool

    import javafx.application.Application; 
    import javafx.scene.Scene; 
    import javafx.scene.control.*; 
    import javafx.scene.layout.*; 
    import javafx.stage.Stage; 
    
    public class SectionalToolbar extends Application { 
        @Override 
        public void start(Stage stage) throws Exception { 
         final Pane leftSpacer = new Pane(); 
         HBox.setHgrow(
           leftSpacer, 
           Priority.SOMETIMES 
         ); 
    
         final Pane rightSpacer = new Pane(); 
         HBox.setHgrow(
           rightSpacer, 
           Priority.SOMETIMES 
         ); 
    
         final ToolBar toolBar = new ToolBar(
           new Button("Good"), 
           new Button("Boys"), 
           leftSpacer, 
           new Button("Deserve"), 
           new Button("Fruit"), 
           rightSpacer, 
           new Button("Always") 
         ); 
         toolBar.setPrefWidth(400); 
    
         BorderPane layout = new BorderPane(); 
         layout.setTop(toolBar); 
    
         stage.setScene(
           new Scene(
             layout 
           ) 
         ); 
         stage.show(); 
        } 
        public static void main(String[] args) { launch(); } 
    } 
    

    ToolBar(至少在标准modena.css样式的Java 8),已经在内部实现为HBox容器,所以可以只使用HBox.setHgrow方法来调整内部您放置在工具栏中的项目的布局约束。

    本例中的左侧和右侧垫片实现为空白窗格,这些窗格只会随着可用空间的增大和缩小而变化。

    如果你想要一个间隔,而不是HBox.setHgrow这是一个固定的大小,你可以使用类似:

    leftSpacer.setPrefSize(20); 
    leftSpacer.setMinSize(HBox.USE_PREF_SIZE); 
    leftSpacer.setMaxSize(HBox.USE_PREF_SIZE); 
    
    +0

    感谢您的支持。我将您的解决方案改编为FXML。在 – burntblark

    0

    将FXML与@jewelsea结合使用。

    <ToolBar prefHeight="40.0" prefWidth="200.0"> 
        <items> 
         <Button mnemonicParsing="false" text="Good" /> 
         <Button mnemonicParsing="false" text="Boys" /> 
         <Pane prefWidth="20.0" HBox.hgrow="SOMETIMES" /> 
         <Button mnemonicParsing="false" text="Deserve" /> 
         <Button mnemonicParsing="false" text="Fruit" /> 
         <Pane prefWidth="20.0" HBox.hgrow="SOMETIMES" /> 
         <Button mnemonicParsing="false" text="Always" /> 
         <Pane prefWidth="20.0" HBox.hgrow="SOMETIMES" /> 
        </items> 
    </ToolBar> 
    
    相关问题