JavaFX(8)警报:不同的按钮大小 [英] JavaFX (8) Alert: different button sizes

查看:211
本文介绍了JavaFX(8)警报:不同的按钮大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑带有两个按钮的JavaFX(8)警告对话框:

Consider a JavaFX (8) Alert dialog with two buttons:

Alert alert = new Alert(AlertType.CONFIRMATION);

ButtonType bttYes = new ButtonType("Yes");
ButtonType bttNo = new ButtonType("No, continue without restart");

alert.getButtonTypes().clear();
alert.getButtonTypes().addAll(bttYes, bttNo);

alert.showAndWait();

生成的对话框有两个按钮,两个宽度相同,看起来很傻。

The resulting dialog has two buttons, both same width which looks quite silly.

有没有办法将两个按钮调整为实际文本长度?

Is there a way to adjust both buttons to the actual text length?

推荐答案

An 警告,它扩展了 Dialog ,包装 DialogPane DialogPane 分为不同的部分,其中一部分是按钮栏。正如预期的那样,此部分是所有按钮的所在位置。按钮栏可以是任意节点,但 DialogPane 的默认实现使用恰当命名的 ButtonBar 控制。这在 <$ c $中有记载。 c> DialogPane.createButtonBar() 强调我的):

An Alert, which extends Dialog, wraps a DialogPane. A DialogPane is split into different sections and one of those sections is the "button bar". This section is, as expected, where all the buttons go. The button bar can can be any arbitrary Node, but the default implementation of DialogPane uses the aptly named ButtonBar control. This is documented in DialogPane.createButtonBar() (emphasis mine):


子类可以重写此方法以提供按钮栏。请注意,通过重写此方法,开发人员必须承担多项责任:

This method can be overridden by subclasses to provide the button bar. Note that by overriding this method, the developer must take on multiple responsibilities:


  1. 开发人员必须立即遍历所有按钮类型并调用createButton(它们依次为每一个提供ButtonType。

  2. 开发人员必须在按钮类型列表中添加一个监听器,当此列表发生更改时,请根据需要更新按钮栏。

  3. 同样,开发人员必须注意对可扩展内容属性的更改,添加和删除详细信息按钮(通过createDetailsButton()方法创建)。

此方法的默认实现创建并返回一个新的ButtonBar实例。

一个 ButtonBar ,默认情况下,将所有按钮的大小调整为最宽的按钮的宽度。 / p>

A ButtonBar, by default, resizes all the buttons to be the width of the widest one.


统一按钮大小调整



默认情况下所有按钮在ButtonBar中统一调整大小,这意味着所有按钮都采用最宽按钮的宽度。可以在每个按钮的基础上选择退出,但是使用布尔值false调用setButtonUniformSize(Node,boolean)方法。

Uniform button sizing

By default all buttons are uniformly sized in a ButtonBar, meaning that all buttons take the width of the widest button. It is possible to opt-out of this on a per-button basis, but calling the setButtonUniformSize(Node, boolean) method with a boolean value of false.

如果a按钮从均匀尺寸中排除,它被排除在远离其首选尺寸的大小,并且也从测量过程中排除,因此其大小不会影响为ButtonBar中的所有按钮计算的最大尺寸。

If a button is excluded from uniform sizing, it is both excluded from being resized away from its preferred size, and also excluded from the measuring process, so its size will not influence the maximum size calculated for all buttons in the ButtonBar.

如上面的Javadoc所述,可以使用静态方法 ButtonBar.setButtonUniformSize(Node,布尔值) 。只需使用 getButtonTypes() lookupButton(ButtonType)循环按钮,并将统一大小设置为 false 每个。

As mentioned in the above Javadoc, this behavior can be disabled with the static method ButtonBar.setButtonUniformSize(Node, boolean). Simply loop over the buttons using getButtonTypes() and lookupButton(ButtonType) and set the uniform sizing to false for each one.

Alert alert = ...;
DialogPane pane = alert.getDialogPane();
pane.getButtonTypes().stream()
        .map(pane::lookupButton)
        .forEach(btn-> ButtonBar.setButtonUniformedSize(btn, false));

请注意,这需要您配置 ButtonType s事先,如果没有使用默认值。

Note that this requires you to have configured the ButtonTypes beforehand, if not using the defaults.

如果涉及自定义 DialogPane ,这将变为实现。例如,自定义 DialogPane 可能不使用 ButtonBar 。但是,您的代码没有显示使用自定义 DialogPane 的指示,因此上述解决方案应该可以正常工作。

This becomes implementation dependent if custom DialogPanes are involved. For instance, maybe the custom DialogPane doesn't use a ButtonBar. However, your code shows no indication of using a custom DialogPane so the above solution should work fine.

这篇关于JavaFX(8)警报:不同的按钮大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆