Java Swing 字体选择器
在我的 Java Swing 应用程序中,我希望允许用户选择他们的字体。令我惊讶的是,没有核心 Swing 库来选择字体。我希望找到一个 JFileChooser 但没有这样的实用程序。这看起来像是一个简单的对话框,我确信它确实存在。我真的不想自己写。
我在互联网上浏览了一下,发现了这种类型对话框的几种不同实现。问题是它们大多数都在随机博客上或者只是代码片段。我正在寻找属于经过良好使用和测试的库的一部分的东西。它还需要拥有商业友好的许可证。
有谁知道可以推荐一个好的 Swing Java 文件选择器吗?
In my Java Swing application I want to allow the user to pick their font. To my surprise there is no core swing library to pick fonts. I expected to find a JFileChooser but there is no such utility. This seems like a simple dialog that I'm sure exists out there. I do not really want to write it myself.
I looked around on the internet and found a few different implementations of this type of a dialog. The problem is most of them are on random blogs or are just code snippets. I am looking for something that is part of a library that is well used and tested. It also needs to have a commercial friendly license.
Does anyone know of a good Java file chooser in swing that they can recommend?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
旧线程,但这是来自 JFontChooser 的代码,删除了所有不必要的资源。将其复制并粘贴到一个新类中,它提供了一些很棒的功能,在我看来,默认情况下应该存在这些功能。
Old thread, but this is the code from JFontChooser, with all the unnecessary resources removed. Copy and paste this into a new class and it provides some great functionality that in my opinion should be there by default.
JFontChooser 看起来相当不错并且是开源的。
JFontChooser looks pretty good and is open-source.
JFontChooser 自 2008 年起不再维护。我创建了一个 Java 9 兼容的开源 Java Swing Font Chooser< /a> 对话框:
您可以使用此工件:
并插入类似这样的内容到你的代码:
或者简单地
JFontChooser is no longer maintained since 2008. I created a Java 9 compatible open source Java Swing Font Chooser dialog:
You can use this artifact:
And insert something like this to your code:
or simply
这是一个带有字体预览和自动完成功能的漂亮组合框。
http://zongmusic.wordpress.com/2010/12/02/fontchoosercombobo/
Here is a nice combobox with font preview and auto-completion.
http://zongmusic.wordpress.com/2010/12/02/fontchoosercombobo/
如果您按照 David 所说的操作,即将其代码复制并粘贴到新类中,则可以调用方法 JFontChooser.showDialog(textArea);,其中 textArea 是您要更改其字体的组件。然后调用 textArea.setFont(JFontChooser.getSelectedFont()); 如果您有 javax.swing.textArea,请准确使用此代码。否则,设置字体的方法可能会有所不同。但它仍然会以同样的方式工作。
If you do what David said, which is copy and paste his code into a new class, you can call the method
JFontChooser.showDialog(textArea);
with textArea being the component you want to change the font of. Then you calltextArea.setFont(JFontChooser.getSelectedFont());
If you have a javax.swing.textArea, use this code exactly. Otherwise the method may be different for setting the font. But it will still work the same way.