java 侦听 ButtonGroup 的“子”更改,并打印选定的 JRadioButton 的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6883604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:43:43  来源:igfitidea点击:

Listening on ButtonGroup for "child" changes, and print selected JRadioButton's text

javaswingjradiobuttonbuttongroup

提问by Tyilo

What I want to is: Create an event that fires if the a JRadioButton contained in the ButtonGroup is selected, and then print the text there is on the JRadioButton.

我想要的是:创建一个事件,如果选择了包含在 ButtonGroup 中的 JRadioButton,则该事件会触发,然后打印 JRadioButton 上的文本。

回答by Hovercraft Full Of Eels

As per my comment, you can't add a listener to a ButtonGroup. You will likely have to go with an ActionListener added to the individual JRadioButtons.

根据我的评论,您不能向 ButtonGroup 添加侦听器。您可能必须将 ActionListener 添加到各个 JRadioButton 中。

If this doesn't answer your question, please tell us more details about your problem.

如果这不能回答您的问题,请告诉我们有关您的问题的更多详细信息。

Edit 1
I suppose you could always extendButtonGroup such that it accepts ActionListeners. For example:

编辑 1
我想你总是可以扩展ButtonGroup 以便它接受 ActionListeners。例如:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.event.EventListenerList;

@SuppressWarnings("serial")
public class MyButtonGroup extends ButtonGroup {
   private ActionListener btnGrpListener = new BtnGrpListener();
   private EventListenerList listenerList = new EventListenerList();

   @Override
   public void add(AbstractButton b) {
      b.addActionListener(btnGrpListener);
      super.add(b);
   }

   public void addActionListener(ActionListener listener) {
      listenerList.add(ActionListener.class, listener);
   }

   public void removeActionListener(ActionListener listener) {
      listenerList.remove(ActionListener.class, listener);
   }

   protected void fireActionListeners() {
      Object[] listeners = listenerList.getListenerList();
      String actionCommand = "";
      ButtonModel model = getSelection();
      if (model != null) {
         actionCommand = model.getActionCommand();
      }
      ActionEvent ae = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, actionCommand);
      for (int i = listeners.length-2; i>=0; i-=2) {
          if (listeners[i]== ActionListener.class) {
              ((ActionListener)listeners[i+1]).actionPerformed(ae);
          }
      }
   }

   private class BtnGrpListener implements ActionListener {

      public void actionPerformed(ActionEvent ae) {
         fireActionListeners();
      }
   }
}

And tested by the following:

并通过以下测试:

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MyButtonGroupTest {
   private static void createAndShowUI() {
      String[] data = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

      JPanel panel = new JPanel(new GridLayout(0, 1));
      MyButtonGroup myBtnGrp = new MyButtonGroup();
      myBtnGrp.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            System.out.println("Action Command is: " + e.getActionCommand());
         }
      });

      for (String text : data) {
         JRadioButton radioBtn = new JRadioButton(text);
         radioBtn.setActionCommand(text);
         myBtnGrp.add(radioBtn);
         panel.add(radioBtn);
      }

      JFrame frame = new JFrame("MyButtonGroupTest");
      frame.getContentPane().add(panel);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      java.awt.EventQueue.invokeLater(new Runnable() {
         public void run() {
            createAndShowUI();
         }
      });
   }
}

But this still eventually adds ActionListers to each JRadioButton to achieve its ends; it just does this behind the scenes in the MyButtonGroup's add method override.

但这最终还是会为每个 JRadioButton 添加 ActionListers 以达到其目的;它只是在 MyButtonGroup 的 add 方法覆盖的幕后执行此操作。