将 Action 侦听器添加到 JComboBox
我想知道如何向 JComboBox 添加 ActionListener。我正在使用AWT。你能给我解释一下吗?谢谢。
I'd like to know how can I add a ActionListener to JComboBox. I'm using AWT. Can you explain it to me? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先注意:awt中没有comboBox项目,Swing中有JComboBox,它比awt的Choice更好用。
或者,您可以定义一个实现 ActionListener 的类,并在其中定义
actionPerformed
方法,完成此操作后,您只需将该类的新实例添加到 JComboBox addActionListener 方法即可。如果其中任何一个对您来说没有意义,您可以查看以下网站:http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html">http:// /download.oracle.com/javase/tutorial/uiswing/components/combobox.html
Note First : there is no comboBox item in awt, there is JComboBox in Swing which is better to use than Choice of awt.
Or you can define a class that implements ActionListener and define the
actionPerformed
method there, and once you do that, you can just add a new instance of that class to your JComboBox addActionListener method.If any of that didn't make sense to you, you can check the follwing website: http://download.oracle.com/javase/tutorial/uiswing/components/combobox.html
除非您仅限于(即小程序),否则不应使用 awt,否则更喜欢 swing 或 swt。
如果您使用
java.awt
中的Choice
,它有一个addItemListener(ItemListener l)
方法。如果您使用 swing,
JComboBox
有一个addActionListener
方法。You should not use awt except you are confined to (i.e. an applet), otherwise prefer swing or swt.
If you use
Choice
fromjava.awt
, it has aaddItemListener(ItemListener l)
method.If you used swing,
JComboBox
has aaddActionListener
method.我不知道有关
AWT ComboBox
的事情,只有 选择,AWT 组件在上个世纪的某个时候悄然诞生,而今天的 GUI 是必需的Swing JComponentsfor JComboBox 你可以实现 ActionListener,但我认为你必须使用这个
Listener
如果您想要/需要更改JComboBox
本身,对于
JComboBox
我建议为JComboBox
提供更好的Listener
>是ItemListener,因为ItemEvent有3个重要状态.SELECTED、DESELECTED 和 ITEM_STATE_CHANGED
注意
JComboBox
有两种状态可编辑和不可编辑编辑:为了头脑清醒和享受编程的乐趣,不要混合另一方面,
AWT Components
和Swing JComponents
可以确保从 JDK1.6_012 开始可以实现更多 此处 和 此处,但是为了向 GUI 提供良好且正确的输出,您必须需要大量有关 Java GUI 的技能I don't know somethig about
AWT ComboBox
there is only Choice, and AWT Componets silently d*i*e*d sometime in last milenium, for todays GUI is required Swing JComponentsfor JComboBox you can implements ActionListener, but I think that you have to use this
Listener
if you want/needed to changeJComboBox
itself,for
JComboBox
I suggest that betterListener
forJComboBox
is ItemListener, because there are three important statesItemEvent.SELECTED, DESELECTED and ITEM_STATE_CHANGED
notice
JComboBox
has two states Editable and non-EditableEDIT: for clear mind and enjoy from programing don't mixing
AWT Components
and withSwing JComponents
, in other hands, sure that possible from JDK1.6_012 more here and here, but for nice and correct output to the GUI you have to needed lots of skills about Java GUI