【问题标题】:Why does deriveFont(float size) not change font size in a JButton?为什么deriveFont(float size) 不改变JButton 中的字体大小?
【发布时间】:2019-12-17 09:13:59
【问题描述】:

deriveFont(float size) 方法只是使Font 变得简单而不改变其大小。

JButton prevButton = new JButton("Previous");
prevButton.setFont(prevButton.getFont().deriveFont(90));

如果我像下面的示例一样使用 derivedFont(int style, float size),它会按预期工作。

JButton prevButton = new JButton("Previous");
prevButton.setFont(prevButton.getFont().deriveFont(Font.BOLD, 90));

有人可以解释这种行为吗?

【问题讨论】:

    标签: java swing fonts jbutton


    【解决方案1】:

    检查所有deriveFont 重载的变量参数。当您deriveFont(90),(有人可能会说它有点模棱两可)时,您正在更改字体的样式而不是大小。方法为deriveFont(int style),其中接受的style值为Font.BOLDFont.ITALICFont.PLAIN

    另一方面,这个方法的另一个重载是deriveFont(float size)。注意float。为了完成这项工作,您应该 @camickr 在 cmets 中指出 deriveFont((float) 90)deriveFont(90f)。将 int 转换为 float 将表明您想要更改大小。

    【讨论】:

    • (1+) 或者你可以使用 90.0f.
    • @camickr 确实如此。谢谢你提到它。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-09
    相关资源
    最近更新 更多