简体   繁体   English

如何在Java中更改字体类型而不更改大小?

[英]How do I change font type but not size in java?

The only way that I have learned to change the font of text in java so far is using new Font(font name, Font.PLAIN, font size) and I was wondering if there is a way that I could just simply change the font type while not effecting any other aspects of it. 到目前为止,我学会更改Java文本字体的唯一方法是使用新的Font(字体名称,Font.PLAIN,字体大小),我想知道是否有一种方法可以简单地更改字体类型同时不影响其任何其他方面。 This is because I want to be able to change the font type of all componenets of the GUI using one method using input from a combo box in the settings section of the program and it would be a pain to go back through all of the components and search for their exact font size. 这是因为我希望能够使用一种方法来更改GUI的所有组件的字体类型,方法是使用程序设置部分中组合框的输入,因此回头查看所有组件和搜索其确切的字体大小。

If you have many components, with different font sizes, register a PropertyChangeListener on each component (that displays text) as you create it. 如果您有许多具有不同字体大小的组件,请在创建它时在每个显示文本的组件上注册一个PropertyChangeListener。 Then the propertyChange() method of the listener should get the font of the source and change it using deriveFont() to create a font of the new style. 然后,侦听器的propertyChange()方法应获取源字体,并使用deriveFont()对其进行更改以创建新样式的字体。

You can use deriveFont(style) to change the font with a new style. 您可以使用deriveFont(style)来更改新样式的字体。 This creates a new Font object. 这将创建一个新的Font对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM