将 Action 侦听器添加到 JComboBox

发布于 2024-11-29 20:18:38 字数 65 浏览 0 评论 0原文

我想知道如何向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2024-12-06 20:18:38

首先注意:awt中没有comboBox项目,Swing中有JComboBox,它比awt的Choice更好用。

JComboBox comboBox = new JComboBox();

comboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when a selection has been made

            }
        });   

或者,您可以定义一个实现 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.

JComboBox comboBox = new JComboBox();

comboBox.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e)
            {
                //Execute when a selection has been made

            }
        });   

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

山川志 2024-12-06 20:18:38

除非您仅限于(即小程序),否则不应使用 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 from java.awt, it has a addItemListener(ItemListener l) method.

If you used swing, JComboBox has a addActionListener method.

南街女流氓 2024-12-06 20:18:38

我不知道有关 AWT ComboBox 的事情,只有 选择,AWT 组件在上个世纪的某个时候悄然诞生,而今天的 GUI 是必需的Swing JComponents

for JComboBox 你可以实现 ActionListener,但我认为你必须使用这个 Listener如果您想要/需要更改 JComboBox 本身,

对于 JComboBox 我建议为 JComboBox 提供更好的 Listener >是ItemListener,因为ItemEvent有3个重要状态.SELECTED、DESELECTED 和 ITEM_STATE_CHANGED

注意 JComboBox 有两种状态可编辑和不可编辑

编辑:为了头脑清醒和享受编程的乐趣,不要混合另一方面,AWT ComponentsSwing 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 JComponents

for JComboBox you can implements ActionListener, but I think that you have to use this Listenerif you want/needed to change JComboBox itself,

for JComboBox I suggest that better Listener for JComboBox is ItemListener, because there are three important states ItemEvent.SELECTED, DESELECTED and ITEM_STATE_CHANGED

notice JComboBox has two states Editable and non-Editable

EDIT: for clear mind and enjoy from programing don't mixing AWT Components and with Swing 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文