2011-11-22 29 views
1

我正在用java创建一个项目。我的程序有80个JRadioButtons ....我需要获取他们的文本值..现在这些单选按钮被添加到ButtonGroup(每个都有4个单选按钮)...如何获取JRadioButton的文本值

我知道如何从文本中获取文本值由以下代码单选按钮

radiobutton1.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       String q1=e.getActionCommand(); 
       JOptionPane.showMessageDialog(null, q1); 
      } 
     }); 

现在有没有简单的方法来做到这一点?因为我将不得不这样做80次以上的代码(对于八十个单选按钮,如果我使用上面的方法使用上述方法

其他信息 - 我有20个ButtonGroups,每个按钮都有4个单选按钮,所以(80个单选按钮)。

+1

我很想看到你与这个庞大的JradioButton将数检查什么。你能告诉我们更多关于这部分代码的作用吗? –

+0

@Hovercraft全部鳗鱼,我的...,我认为JSlider应该不仅仅是一个替代品, – mKorbel

+0

@Hovercraft全部鳗鱼我创建Faculty反馈系统...他们专门告诉我们使用RadioButtons ...有每个问题有四个选项......总共有20个问题。 –

回答

2

可能而不是定义动作监听每个单选按钮分别,你应该确定一个共同的动作监听所有的单选按钮。

public class RadioActionListener implements ActionListener { 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     //String q1=e.getActionCommand(); 

     //Use the ActionEvent#getSource() method which gives you the reference to the 
     //radio-button that caused the event 
     JRadioButton theJRB = (JRadioButton) e.getSource(); 
     JOptionPane.showMessageDialog(null, theJRB.getText()); 
    } 
} 

然后,您可以使用它如下:

ActionListener radioAL = new RadioActionListener(); 

radiobutton1.addActionListener(radioAL); 
radiobutton2.addActionListener(radioAL); 

此外,ActionEvent#getActionCommand()返回与动作不extacly命令组件的文本相关联的命令字符串。

+0

没有必要创建80个RadioActionListeners。创建一个并为所有单选按钮分享它。这就是为什么你编写泛型代码来从ActionEvent获取事件的来源。 – camickr

+0

@camickr:你说得对。编辑。谢谢。 –

2

你面临这个问题的原因是因为你手动创建了每个JRadioButton,我猜(而不是一个循环)。

如果你真的不能做到这一点,否则,你可以使用一些智能代码:

Container c = ...; // The component containing the radiobuttons 
Component[] comps = c.getComponents(); 
for (int i = 0; i < c.getComponentCount(); ++i) 
{ 
    Component comp = comps[i]; 
    if (comp instanceof JRadioButton) 
    { 
     JRadioButton radiobutton = (JRadioButton) comp; 
     // add the listener 
     radio.addActionListener(...); 
    } 
} 
3
I have Total 20 ButtonGroups each with 4 radio buttons. So(80 radio buttons). 

那么最简单的方法是

String actionCommand = ""; 
ButtonModel buttonModel = myButtonGroup.getSelection(); 
if (buttonModel != null) { 
    actionCommand = buttonModel.getActionCommand(); 
} else { 
    // buttonModel is null. 
    // this occurs if none of the radio buttons 
    // watched by the ButtonGroup have been selected. 
} 
+0

这可以很好地工作,所以1+,但是你必须注意检查getSelection()返回的ButtonModel在调用getActionCommand()之前是否为空。如果您不介意,我想编辑您的文章以反映这一点。此外,由于getActionCommand()返回一个String,所以不需要对'getActionCommand()'返回的值调用'toString()'。 –

+0

@@ Hovercraft鳗鱼感谢卓越的建议... – mKorbel

+0

@Hovercraft完整的鳗鱼字符串actionCommand不打印JRadiobutton的文本,但它打印空..它进入(如果buttonModel!= null)条件..然后它打印空...我的代码如下(如果(buttonModel!= null){actionCommand = buttonModel.getActionCommand(); System.out.println(actionCommand);} –

1

的关键在于实现像你这样的设计欲望(我认为)是使用阵列来发挥他们最大的力量。例如,您可以拥有一个包含JRadioButton文本的二维String数组和一个ButtonGroups的一维数组,然后可以轻松设置您的GUI并使用for循环和嵌套for循环查询GUI(以及使用mKorbel的出色建议)。

例如:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class Foo002 extends JPanel { 
    public static final String[][] RADIO_TEXTS = { 
     {"A1","A2","A3","A4"}, {"B1","B2","B3","B4"}, 
     {"C1","C2","C3","C4"}, {"D1","D2","D3","D4"}, 
     {"E1","E2","E3","E4"}, {"F1","F2","F3","F4"}, 
     {"G1","G2","G3","G4"}, {"H1","H2","H3","H4"}, 
     {"I1","I2","I3","I4"}, {"J1","J2","J3","J4"}, 
     {"K1","K2","K3","K4"}, {"L1","L2","L3","L4"}, 
     {"M1","M2","M3","M4"}, {"N1","N2","N3","N4"}, 
     {"O1","O2","O3","O4"}, {"P1","P2","P3","P4"}, 
     {"Q1","Q2","Q3","Q4"}, {"R1","R2","R3","R4"}, 
     {"S1","S2","S3","S4"}, {"T1","T2","T3","T4"} 
     }; 

    private ButtonGroup[] btnGroups = new ButtonGroup[RADIO_TEXTS.length]; 

    public Foo002() { 
     JPanel radioPanel = new JPanel(new GridLayout(0, 2)); 
     for (int i = 0; i < RADIO_TEXTS.length; i++) { 
     JPanel panel = new JPanel(new GridLayout(1, 0)); 
     btnGroups[i] = new ButtonGroup(); 
     for (int j = 0; j < RADIO_TEXTS[i].length; j++) { 
      String text = RADIO_TEXTS[i][j]; 
      JRadioButton rBtn = new JRadioButton(text); 
      rBtn.setActionCommand(text); 
      btnGroups[i].add(rBtn); 
      panel.add(rBtn); 
     } 
     panel.setBorder(BorderFactory.createLineBorder(Color.black)); 
     radioPanel.add(panel); 
     } 

     JButton getRadioChoicesBtn = new JButton(new AbstractAction("Get Radio Choices") { 
     public void actionPerformed(ActionEvent arg0) { 
      for (ButtonGroup btnGroup : btnGroups) { 
       ButtonModel btnModel = btnGroup.getSelection(); 
       if (btnModel != null) { 
        System.out.println("Selected Button: " + btnModel.getActionCommand()); 
       } 
      } 
     } 
     }); 
     JPanel btnPanel = new JPanel(); 
     btnPanel.add(getRadioChoicesBtn); 

     setLayout(new BorderLayout()); 
     add(radioPanel, BorderLayout.CENTER); 
     add(btnPanel, BorderLayout.SOUTH); 
    } 

    private static void createAndShowGui() { 
     JFrame frame = new JFrame("RadioPanels"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(new Foo002()); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 

} 
+0

好,很好,谢谢皮特+1 – mKorbel

+0

在技术上是正确的,但是...嘿,我们在OO土地。用一系列数组来模拟一堆带有选择的问题是......历史上的前 kleopatra

+0

@kleopatra:同意。这仅仅是一个概念验证的SSCCE。在现实生活中,这些问题不是代码的一部分,而是数据的一部分。 –