将按钮文本缩放到按钮大小(反之亦然)

时间:2015-05-25 08:46:26

标签: javafx-8 alerts platform-specific

我有一个简单的警告,如下所示,在Windows和Linux中显示不同。有很多帖子可以根据文字缩放按钮大小,但我基本上想要相反。按钮应保持相同的大小,文本应缩放以适合(就像它似乎在窗口中做的那样)

protected static void setDifficulty(){

    Alert alert = new Alert(AlertType.CONFIRMATION);
    alert.setTitle("Welcome to the game of Memory");
    alert.setHeaderText("How difficult should your oppenent be?");
    alert.setContentText("Please choose your desired difficulty.");

    ButtonType button1 = new ButtonType("Nice n' Easy");
    ButtonType button2 = new ButtonType("So So");
    ButtonType button3 = new ButtonType("Super Freak");
    ButtonType buttonCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);

    alert.getButtonTypes().setAll(button1, button2, button3, buttonCancel);

    Optional<ButtonType> result = alert.showAndWait();
    if (result.get() == button1){
        MemoryField.difficulty = 10;
    } else if (result.get() == button2) {
        MemoryField.difficulty = 5;
    } else if (result.get() == button3) {
        MemoryField.difficulty = 0;
    } else {
        Platform.exit();
    }
}

在Windows上:

enter image description here

在Ubuntu上:

enter image description here

1 个答案:

答案 0 :(得分:0)

我能够在Linux上避免此问题的唯一方法是获得按钮的手柄,并设置首选宽度:

    for(ButtonType buttonType : alert.getButtonTypes()){
        Button button = (Button) alert.getDialogPane().lookupButton(buttonType);
        button.setPrefWidth(100);
    }