将ActionListener添加到Panel - Panel实现ActionListener vs Panel HAS ActionListener [英] Adding ActionListener to a Panel - Panel implements ActionListener vs Panel HAS A ActionListener

查看:122
本文介绍了将ActionListener添加到Panel - Panel实现ActionListener vs Panel HAS ActionListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的节目制作了一个小组。它仅由RadioButtons组成。当选择radiobutton时,我想在其他代码中设置一个布尔值。此面板将用作更大面板或框架的组件,该面板或框架也应该能够收听此面板内发生的事件。

I made a panel for my program. It consists of RadioButtons only. When a radiobutton is selected, I want to set a boolean in other code. This panel will be used as a component of a bigger panel or frame which should also be able to listen to the events happening inside this panel.

那么,以下哪个我应该选择选择收听活动 -

So, which of the following options should I choose for listening to events -

1 -

RadioButtonPanel extends JPanel implements ActionListener{

  public void actionPerformed(ActionEvent e){}
  //code to add the action listener to the radio buttons 
  oneRadioButton.addActionListener(this);   
}

2 -

RadioButtonPanel extends JPanel{

  class InnerStrength implements ActionListener{
     public void actionPerformed(ActionEvent e){} 
   }    

  //code to add the action listener to the radio buttons     
  oneRadioButton.addActionListener(Anonymous InnerStrength) 

}

3 - 其他任何我想不到的方法吗?

3 - Any other way to do it that I did not think of ?

推荐答案

在面板中添加 add / RemoveActionListener 方法。使用这些作为您的代理单选按钮并直接向其注册 ActionListener

Add a add/RemoveActionListener method to your panel. Use these as proxy to you radio button and register the ActionListener directly to it

或者,您可以提供 isSelected 方法,它返回单选按钮的状态。

Alternatively, you could provide a isSelected method which returns the state of the radio button.

这篇关于将ActionListener添加到Panel - Panel实现ActionListener vs Panel HAS ActionListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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