为什么 deriveFont(float size) 不更改 JButton 中的字体大小?

Why does deriveFont(float size) not change font size in a JButton?

deriveFont(float size) 方法只是在不改变其大小的情况下使 Font 变得简单。

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

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

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

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

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

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