2011-11-22 56 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。在現實生活中,這些問題不是代碼的一部分,而是數據的一部分。 –