在 Swing 中,在文本区域中使用的最佳侦听器是什么
我正在尝试更改 Swing 中文本区域中文本的字体。我应该在文本区域上使用哪个侦听器来触发让程序启动字体代码的操作。
所有示例都在同一个类中包含所有 swing,让您可以直接访问文本区域,但我有多个类;我知道我可以传入和传入文本区域,但这很草率。
我只是不知道该启动哪个监听器。
I am trying to change the font of the text in a textarea in Swing. Which listener should I use on textarea to trigger an action that lets the program initiate the font code.
All the examples have all the swing in the same class which lets you access the textarea directly, but I have multiple classes; I know I can pass the textarea in and in and in, but this is sloppy.
I just cannot figure out which listener to initiate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么 JTextArea 只能有一个字体,因此如果您想更改字体,您将有一些其他组件,也许您可以单击“更改字体”按钮。在这种情况下,您将向按钮添加一个 ActionListener 以更改文本区域的实际字体。
如果您确实需要更改选定文本片段的字体,那么您也无法使用 JTextArea 来执行此操作。您需要使用 JTextPane。阅读 JTextPane API 并点击有关“文本组件功能”的 Swing 教程的链接,获取更改所选文本属性的示例。为此,您需要使用编辑器工具包提供的操作。
因此基本上您需要阅读 Swing 教程来了解使用 Swing 组件的基础知识。
Well a JTextArea can only have a single Font, so if you want to change the Font you would have some other component, maybe a "Change Font" button that you would click. In this case you would add an ActionListener to the button to change the actual Font of the text area.
If you actually need to change the Font on selected pieces of text, then you also can't do this with a JTextArea. You would need to use a JTextPane. Read the JTextPane API and follow the link to the Swing tutorial on "Text Component Features" for an example of changing attributes on selected text. In this cause you use Actions provided by the editor kit.
So basically you need to read the Swing tutorial to find out the basics of using Swing components.
如果您正在收听文本区域,那么这取决于您希望用户能够以多少种不同的方式更改他们正在键入的内容的字体。
如果您希望它们能够在右键单击/等时更改字体,您可以使用 MouseListener...或者如果您想监听一系列按键,则可以使用 KeyListener。
If you're listening to the textarea, then it would depend on how many different ways you want the user to be able to change the font of what they are typing.
You could use MouseListener if you want them to be able to change the font on right click/etc... or a KeyListener if you want to listen for a series of keys.