本文介绍了gui和actionlistener的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道什么是把actionlistener放在java中的最佳实践?例如在我的类a是我的gui,(设计)然后类b将是我的actionlistener?或者是一个页面中的所有更好?
感谢。

I would know know what is the best practice for putting actionlistener in java? for example in my class a is my gui, (design) then class b would be my actionlistener? or is it better in one one page all of them?thanks.

推荐答案

这取决于。如果你想使用你的 ActionListener 几个UI组件:按钮,菜单项,...那么它是适当的在一个单独的类。另外,如果 actionPerformed 方法中的代码有很多行,那么单独做它是有意义的。

It depends. If you want to use your ActionListener for several UI components: button, menu item,... then it would be appropriate to do it in a separate class. Also, if the code inside the actionPerformed method is of a lot of lines it makes sense to do it separately.

否则,如果您的类中没有很多UI组件,那么您可以将 ActionListener 定义为匿名实现,并将其直接附加到您的组件。 p>

Otherwise, if you don't have a lot of UI components in your class then you can define the ActionListener as an anonymous implementation and attach it to your component directly.

button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    // ...
  }
});

这篇关于gui和actionlistener的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 04:57