【问题标题】:JAVAFX Stage : Default button selection and alignment with text paddingJAVAFX Stage:默认按钮选择和文本填充对齐
【发布时间】:2020-08-04 05:28:08
【问题描述】:

我是 Javafx 的新手,正在尝试为用户实现警告/确认对话框。

我可以得到对话框,但无法按要求对齐。

以下是我的对话框当前的外观:

现在,我需要进行两项更改,

  1. 我想要并排放置按钮,首先是yes,然后是no。但默认情况下应选择“否”。这样在关闭时 No 作为响应发送。
  2. 我想在文本和按钮之间留出一些空间。如果文本大小增加,是否可以将文本分成两行?

以下是我当前的代码:

public static boolean display(String title, String message){
        Stage window = new Stage();
        window.initModality(Modality.APPLICATION_MODAL);
        window.setTitle("Confirmation");
        window.setMinWidth(540);
        window.setMinHeight(250);
        Label label = new Label();
        label.setText(message);

        Button yesButton = new Button("Yes");
        Button noButton = new Button("no");

        yesButton.setOnAction(e->{
            answer = true;
            window.close();
        });

        noButton.setOnAction(e->{
            answer = false;
            window.close();
        });
        
        VBox layout = new VBox(10);
        layout.getChildren().addAll(label,noButton,yesButton);
        layout.setAlignment(Pos.CENTER);
        Scene scene = new Scene(layout);
        window.setScene(scene);
        window.showAndWait();
        return answer;
    }

请帮忙。提前致谢。

【问题讨论】:

  • 完成布局教程

标签: javafx vbox


【解决方案1】:

我想要并排放置按钮,先是,然后是否

将按钮放入 HBox

默认情况下应选择否。这样在关闭时 No 作为响应发送。

类似问题请见here

我想在文本和按钮之间留一些空间。

在 HBox 的顶部添加一些边距,您将把按钮放入

HBox h = //...
HBox.setMargin(h, new Insets(20, 0, 0, 0));

如果文本大小增加,是否也可以将文本分成两行?

label.setWrapText(true);

【讨论】:

    【解决方案2】:

    Button类不支持.setSelected方法,所以我们必须使用ToggleButton类:

        ToggleButton yesToggleButton = new ToggleButton("Yes");
    
        ToggleButton noToggleButton = new ToggleButton("No");
    
        noToggleButton.setSelected(true);
    

    您还可以看到,Toggle 一词出现在 ToggleButton 类中,这意味着可以选择或不选择按钮。


    现在我们必须将ToggleButton 类的objects 分配给ToggleGroup 对象,因为ToggleButton 类的objects 可以由ToggleGroup 类管理:

    ToggleGroup toggleGroup = new ToggleGroup();
    
    yesToggleButton.setToggleGroup(toggleGroup);
    
    noToggleButton.setToggleGroup(toggleGroup);
    

    要创建与按钮的距离,我们现在必须指定 abscissa axisordinate axis 的坐标:

        yesToggleButton.setTranslateX(-100); // abscissa axis
        yesToggleButton.setTranslateY(25);  // ordinate axis
    
        noToggleButton.setTranslateX(100); // abscissa axis
        noToggleButton.setTranslateY(25); // ordinate axis
    

    如果字体变大,字体不再可读,可以使用.setWrapText方法在另一行显示:

    messageType.setFont(Font.font(20)); // sets the font size
    
    messageType.setWrapText(true);
    

    您使用了VBox 类,它垂直对齐objects。由于您希望objects 是水平的,您可以使用HBox,或者在任何方向对齐objects,我会推荐StackPane

    StackPane root = new StackPane();
    

    我会向你推荐这种设置场景的方法:

    Stage confirmationWindow = new Stage();
    
    confirmationWindow.setScene(new Scene(root, 540, 323));
    

    【讨论】:

    • 感谢@Mikail 的帮助。这真的很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    相关资源
    最近更新 更多