编写 Swing 组件:如何添加添加 ActionListener 的功能?
我想通过组合几个现有组件来创建一个(希望是简单的)自定义 Swing 组件。就我而言,它是一个开关,由一个 JLabel 和两个用于打开和关闭的 JButton 组成。我通过扩展 JPanel 开始 OnOffSwitch。构造函数添加子组件,并将其自身设置为按钮的 ActionListener。该类有一个 isOn() 方法用于查询组件的当前状态。
我现在想要添加将 ActionListener 添加到 OnOffSwitch 类的功能。我希望通过扩展像 JPanel 这样的 Swing 组件来免费提供此功能,但 JPanel 没有此功能。从源代码的外观来看,每个具有此功能的 Swing 组件都会重新实现它本身:将侦听器添加到列表、触发 ActionEvents 等。
实现我想要的目标的正确方法是什么?我可以从各种 Swing 组件中复制/粘贴该代码(或重写其要点),或者我可以实现我自己的 OnOffSwitchListener 接口。为了保持一致,我的所有组件似乎都应该使用 ActionListener。
I want to create a (simple, hopefully) custom Swing component by composing several existing components. In my case, it is an on-off switch which consists of a JLabel, and two JButtons for On and Off. I begin OnOffSwitch by extending JPanel. The constructor adds the sub-components, and sets itself up as an ActionListener for the buttons. The class has an isOn() method for querying the current state of the component.
I now want to add the ability to add ActionListeners to the OnOffSwitch class. I expected this functionality would come for free by having extended a Swing component like JPanel, but JPanel does not have this functionality. By the looks of the sources, every Swing component which does have this functionality re-implements it itself: the adding listeners to the list, the firing of ActionEvents, etc.
What is the correct way to achieve what I want? I can copy/paste that code from the various Swing components (or re-write the gist of it), or I can implement my own OnOffSwitchListener interface. To be consistent it seems that all my components should use ActionListeners.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用
JToggelButton
,如图所示 此处,或委托给包含的按钮,如 @duffymo 建议的那样。如果您确实需要自定义OnOffSwitchEvent
,则标准接线概述于EventListenerList
,每个JComponent
中都包含其实例。附录:以下是委托给包含两个按钮的
ButtonGroup
的示例。标签用符号装饰,但Icon
的任何实现都更加灵活。I'd use a
JToggelButton
, as shown here, or delegate to the contained buttons, as @duffymo suggests. If you really need a customOnOffSwitchEvent
, the standard wiring is outlined inEventListenerList
, an instance of which is contained in everyJComponent
.Addendum: Here's an example of delegating to a
ButtonGroup
containing two buttons. The label is decorated with a symbol, but any implementation ofIcon
is even more flexible.我个人认为您不需要自定义 Swing 组件。您的 UI 类不需要扩展任何 Swing 类;您不太可能提供太多自定义行为。 (我可能会同意 JPanel 可以组合其他面板。)
我更喜欢组合而不是继承。拥有一个包含 Swing 数据成员的 UI 类,并为其提供根据需要添加和删除侦听器的方法。您可以通过这种方式更改行为,而无需重写 UI 类。它只不过是一个容器。
I personally don't think you need a custom Swing component. No need for your UI class to extend any Swing class; you aren't likely to provide much custom behavior. (I might concede for a JPanel that composes others.)
I would prefer composition over inheritance. Have a UI class that has Swing data members and give it methods to add and remove Listeners as you need them. You can change the behavior that way without having to rewrite the UI class. It's nothing more than a container.
是的。这就是您在编写自定义 Swing 组件时必须做的事情。
正如您所说,您可以从现有的 Swing 组件中复制
ActionListener
代码,Yep. That's what you have to do when you're writing a custom Swing component.
As you say, you can copy the
ActionListener
code from an existing Swing component,