【问题标题】:JavaFX Separator depend not on the Width of the ClassJavaFX 分隔符不依赖于类的宽度
【发布时间】:2014-11-25 14:58:42
【问题描述】:

我想要一个分隔符,它可以根据母组件的大小来改变他的大小。 在我的示例中,我有一个 JavaFX 弹出窗口,并在那里添加了一个 VBox。在这个 VBox 中,我添加了一个 HBox。而这个 HBox 有一个标签、一个分隔符和一个按钮。 现在我想让按钮在右端,标签在 HBox 的左端。我想我必须在这些组件之间使用分隔符来获得空间。

我该如何处理...

我做了这样的东西,但它不起作用。

// Box for the Headline
    HBox headLine = new HBox();
    headLine.setPadding(new Insets(5, 5, 5, 5));

    // Label with the HeadLine Description in
    final Label heading = new Label(headLineText);
    heading.getStyleClass().addAll("popup-label-name");

    // Close Button
    close = new Button("X");
    close.setVisible(false);
    closeButtonHandler();

    // Creates an invisble Separator1
    Separator sep = new Separator(Orientation.HORIZONTAL);
    sep.setVisible(false);
    sep.widthProperty().add(m_container.widthProperty().get());

    close.getStyleClass().addAll("popup-button", "popup-button-color");

    // Adds to the Headline the Data
    headLine.getChildren().addAll(heading, sep, close);

变量 m_container 是 VBox!我该如何处理?

感谢您的帮助:)

【问题讨论】:

  • 您只需要空格吗?还是可见的分隔符?

标签: properties javafx width separator


【解决方案1】:

最简单的方法(如果不使用 AnchorPane 等其他容器)是插入一个不可见但可扩展的“空间”对象:

void testLabelSpace(HBox box) {        
    Text first = new Text("first");
    Text second = new Text("second");

    Node space = new HBox();      
    HBox.setHgrow(space, Priority.ALWAYS);

    box.getChildren().addAll(first, space, second);
}

【讨论】:

    【解决方案2】:

    如果我正确理解了这个问题,您只需要标签和按钮之间的空格。只需告诉Label 始终水平增长,并设置其最大宽度以使其可以增长到任意大小:

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    import javafx.stage.Stage;
    
    public class HBoxExample extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            HBox hbox = new HBox();
            Label label = new Label("Label");
            Button button = new Button("Button");
            HBox.setHgrow(label, Priority.ALWAYS);
            label.setMaxWidth(Double.MAX_VALUE);
            hbox.getChildren().addAll(label, button);
    
            primaryStage.setScene(new Scene(hbox, 350, 75));
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      • 2019-04-08
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      相关资源
      最近更新 更多