使选定的单选按钮显示在文本区域中

Have the selected radio button to display in text area

我正在 netbeans 中制作一个程序,允许用户导入他们的计算机规格,我有 2 个单选按钮,我想要它,当你 select 选择时,当按下显示时,它会显示你的选择在文本区域。我已经有其他文本字段,人们可以在这些文本字段中输入信息,这些文本字段会显示在文本区域中,但是我该如何为单选按钮执行此操作。

  private void displayActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        textarea.append("Processor: " + processorTextField.getText() + "\nGraphics Card: "
           + graphicsCardTextField.getText() + "\nRam: " + ramTextField.getText() + "\nHard Drive: " + 
                   hardDriveTextField.getText() + "\nOperating System: " + operatingSystemTextField.getText()
                   + "\nMonitor Size: " + monitorTextField.getText());
    }  

这是我已有的代码,用于在按下显示按钮时让其他文本字段进入文本区域

如果您已将 JRadioButton 添加到 ButtonGroup,则 ButtonGroup 可以通过调用 getSelection() 从选定的 JRadioButton 中为您提供 ButtonModel。然后您可以获得模型的 actionCommand 字符串(必须为 JRadioButtons 显式设置)。例如,假设一个名为 buttonGroup:

的 ButtonGroup
private void displayActionPerformed(java.awt.event.ActionEvent evt) { 

    // 1st get the ButtonModel for the selected radio button
    ButtonModel buttonModel = buttonGroup.getSelection();

    // if a selection has been made, then model isn't null
    if (buttonModel != null) {  
        // again actionCommand needs to be set for each JRadioButton
        String actionCommand = buttonModel.getActionCommand();
        // TODO: use actionCommand String as needed
    }
}

例如:

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class RadioEg extends JPanel {
   private static final String[] RADIO_TEXTS = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
   private ButtonGroup buttonGroup = new ButtonGroup();

   public RadioEg() {
      JPanel radioPanel = new JPanel(new GridLayout(0, 1));
      for (String radioText : RADIO_TEXTS) {
         JRadioButton radioButton = new JRadioButton(radioText);
         radioButton.setActionCommand(radioText); // set this!
         radioPanel.add(radioButton); // add to JPanel
         buttonGroup.add(radioButton); // add to button group
      }

      JPanel southPanel = new JPanel();
      southPanel.add(new JButton(new AbstractAction("GetSelection") {

         @Override
         public void actionPerformed(ActionEvent e) {
            ButtonModel buttonModel = buttonGroup.getSelection();
            if (buttonModel != null) {
               String actionCommand = buttonModel.getActionCommand();
               System.out.println("Selected Button: " + actionCommand);
            }
         }
      }));

      setLayout(new BorderLayout());
      add(radioPanel, BorderLayout.CENTER);
      add(southPanel, BorderLayout.PAGE_END);
   }

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

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