Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 收听按钮组中的“输入”;“儿童”;更改,并打印选定的JRadioButton';s文本_Java_Swing_Jradiobutton_Buttongroup - Fatal编程技术网

Java 收听按钮组中的“输入”;“儿童”;更改,并打印选定的JRadioButton';s文本

Java 收听按钮组中的“输入”;“儿童”;更改,并打印选定的JRadioButton';s文本,java,swing,jradiobutton,buttongroup,Java,Swing,Jradiobutton,Buttongroup,我想说的是: 创建一个事件,如果选择了ButtonGroup中包含的JRadioButton,则会触发该事件,然后打印JRadioButton上的文本。根据我的评论,您不能向ButtonGroup添加侦听器。您可能需要在各个JRadioButtons中添加一个ActionListener 如果这不能回答您的问题,请告诉我们您的问题的更多细节 编辑1 我想您总是可以扩展ButtonGroup,使其接受ActionListeners。例如: import java.awt.event.ActionE

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

根据我的评论,您不能向ButtonGroup添加侦听器。您可能需要在各个JRadioButtons中添加一个ActionListener

如果这不能回答您的问题,请告诉我们您的问题的更多细节

编辑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();
      }
   }
}
并通过以下方法进行测试:

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();
         }
      });
   }
}
但这最终还是会在每个JRadioButton中添加ActionLister以实现其目的;它只是在MyButtonGroup的add方法覆盖中的后台执行此操作