【问题标题】:Have the selected radio button to display in text area让选定的单选按钮显示在文本区域中
【发布时间】:2015-05-02 01:45:04
【问题描述】:

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

  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());
    }  

这是当按下显示按钮时其他文本字段进入文本区域的代码

【问题讨论】:

    标签: java swing actionlistener jradiobutton


    【解决方案1】:

    如果您已将 JRadioButtons 添加到 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();
             }
          });
       }
    }
    

    【讨论】:

    • 如何将两个 JRadioButtons 放入一个 ButtonGroup?
    • @HarpreetSunner:通过调用add(...) 方法。例如:buttonGroup.add(myRadioButton)。请参阅上面的代码示例。
    • 很抱歉让我这么痛苦,但我已经将它们添加到一个组中,但是我如何将你给我的代码放入我已经得到的代码中?
    • @HarpreetSunner:这是你自己想办法。 :) 我完全相信你可以做到这一点,但首先不要使用我的代码——你不应该以这种方式借用代码——而是通过研究它,并使用它的ideas.
    猜你喜欢
    • 2016-10-30
    • 1970-01-01
    • 2015-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多