如何使用actionPerformed(ActionEvent e)多一个按钮? [英] How to use actionPerformed( ActionEvent e ) with more that one button?

查看:172
本文介绍了如何使用actionPerformed(ActionEvent e)多一个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的任务是创建一个顺序文件。我的教授给了我这个简单的动作代码:

So my assignment says to create a sequential file. My professor gave me this simple code for the actionperformed:

    public void actionPerformed( ActionEvent e )  { 

    //FOR STATE AND COUNTRY
    String country = (String)comboBox_1.getSelectedItem();
        Object o = states.get( country );

        if (o == null)
        {
            comboBox_2.setModel( new DefaultComboBoxModel() );
        }
        else
        {
            comboBox_2.setModel( new DefaultComboBoxModel( (String[])o ) );
        }
        //****DONE WITH THE STATE AND COUNTRY COMBOBOXEZ*****

    addRecord( ) ;



    if ( e.getSource( ) == btnDone )  {
      try {
        output.close( ); 
      }
      catch ( IOException io )  {
        System.err.println( "File not closed properly\n" +
           e.toString( ) );
        System.exit(1);
      }



      System.exit(0); 


    }
  } 

}

它基本上说如果你点击完成按钮之外的任何东西,它会创建一个顺序文件。我该怎么做才能选择每个动作会做什么?我和国家和州一起做了一个jcombobox,当我选择一个国家时,它会创建文件,然后它就会把我带到这个国家的州。希望你可以帮我谢谢。

}
it basically says if you hit anything but the "Done" button, it creates a sequential file. What should i do to choose what each action will do? i did a jcombobox with countries and states, and when i choose a country, it creates the file and then it gets me to the country's states.Hope you can help me out thanks.

推荐答案

我不确定我完全理解你的问题,但是......

I'm not sure I entirely understand your problem, but...

你可以使用:


  • ActionEvent#getSource 获取事件的源组件。假设您可以引用其他组件,您只需使用它来比较它们,例如 if(e.getSource()== btnDone){

  • ActionEvent#getActionCommand 返回与组件关联的 actionCommand (通过设置> setActionCommand 关于支持组件),它提供了确定事件类型的方法,无需引用源组件,当您可能有一个可以触发的公共操作时,这也很有用。多种不同的方式。

  • ActionEvent#getSource to get the source component of the event. Assuming that you can reference the other components, you can simply use this to compare them, for example if (e.getSource( ) == btnDone) {
  • ActionEvent#getActionCommand which returns the actionCommand associated with the component (set via the setActionCommand on supporting components), which provides you means of determining the type of event, without needing a reference to the source components, this is also useful when you might have a "common" action that can triggered in multiple different ways.

现在,一般来说,当调用 actionPerformed 时,你想确定什么触发了行动并采取了适当的行动,所以在你的代码的情况下,你可能会做更多的事情......

Now, generally speaking, when actionPerformed is called, you want to determine what triggered the action and take appropriate action, so the case of your code, you might do something more like...

if (e.getSource() == comboBox_1) {
    String country = (String) comboBox_1.getSelectedItem();
    Object o = states.get(country);

    if (o == null) {
        comboBox_2.setModel(new DefaultComboBoxModel());
    } else {
        comboBox_2.setModel(new DefaultComboBoxModel((String[]) o));
    }
    //****DONE WITH THE STATE AND COUNTRY COMBOBOXEZ*****

    addRecord();
} else if (e.getSource() == btnDone) {
    try {
        output.close();
    } catch (IOException io) {
        System.err.println("File not closed properly\n"
                + e.toString());
        System.exit(1);
    }

    System.exit(0);

}

这是一种相当老的方式来设计 ActionListener ,可以追溯到内部/匿名类,其中更简单的是创建一个 ActionListener 类。

This is a rather "old" way to approach designing ActionListener, which harks back to the days before inner/anonymous classes, where is was just simpler to create a single ActionListener class.

现在通常更倾向于使用内部/匿名类来生成小型,隔离和上下文的侦听器,这可能看起来像......

Now days it's generally more preferable to use inner/anonymous classes to generate small, isolated and contextual listeners, which might look something like...

comboBox_1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        String country = (String) comboBox_1.getSelectedItem();
        Object o = states.get(country);

        if (o == null) {
            comboBox_2.setModel(new DefaultComboBoxModel());
        } else {
            comboBox_2.setModel(new DefaultComboBoxModel((String[]) o));
        }
        //****DONE WITH THE STATE AND COUNTRY COMBOBOXEZ*****

        addRecord();
    }
});

这是一个独立的上下文单元工作,专为单次使用工作而设计。一般来说,它更易于阅读和维护,因为上下文定义明确,而且你并没有试图围绕一堆无关的工作。

This is a self contained, contextual unit work, designed for a single use of work. Generally it's easier to read and maintain as the context is well defined and you're not trying to tip-toe around a bunch of unrelated work.

如果你需要的东西是更多可重用,那么你应该看看如何使用动作

If you need something that's more re-usable, then you should have a look at How to Use Actions

这篇关于如何使用actionPerformed(ActionEvent e)多一个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆