0

嗯,它很奇怪。顺便说一句,我不擅长单选按钮。但是我在 netbeans 中制作了一个 JPanel 程序,其中包括一个 RadioButton。您使用 JTextFields 输入所有这些信息(没问题),最后我有一个 JButton,您可以单击所需的选项。然后我有一个 JButton,它获取所有信息并输出它。对于 RadioButton,我首先输入了通常的:

    family = new JRadioButton("Family", true);
    friend = new JRadioButton("Friend");
    relative = new JRadioButton("Relative");
    friendFriend = new JRadioButton("Friend of Friend");

    ButtonGroup group = new ButtonGroup();
    group.add (friend);
    group.add (family);
    group.add (relative);
    group.add (friendFriend);

(我不确定我是否需要 RadioButtons 的侦听器,但无论如何我的程序似乎仍然“崩溃”)。

然后我有一个用于 JButton 的操作侦听器,其中包括所有文本字段和单选按钮。但是 RadioButton 是问题所在。

在我的动作监听器中: Object source = event.getSource();

        if (source == family)
            relation1 = true;
        else
            if (source == friend)
                relation2 = true;
            else
                if(source == relative)
                    relation3 = true;
                else
                    if(source == friendFriend)
                    relation4 = true;

然后我做了一个关系类: public class Relation { private boolean arrayFamily, arrayFriend, arrayRelative, arrayFriendFriend;

public Relation(boolean relation1, boolean relation2, boolean relation3,
        boolean relation4)
{
    this.arrayFamily = relation1;
    this.arrayFriend = relation2;
    this.arrayRelative = relation3;
    this.arrayFriendFriend = relation4;
}

public String relations ()
{
    String relationship = null;

    if(arrayFamily && !arrayFriend && !arrayRelative && !arrayFriendFriend == true)
    {
        relationship = "Family";
    }
    else
        if(arrayFriend && !arrayFamily && !arrayRelative && 
                !arrayFriendFriend == true)
        {
            relationship = "Friend";
        }
        else
            if(arrayRelative && !arrayFamily && !arrayFriend && 
                    !arrayFriendFriend == true)
            {
                relationship = "Relative";
            }
            else
                if(arrayFriendFriend && !arrayFamily && !arrayFriend &&
                        !arrayRelative == true)
                {
                    relationship = "Friend of a Friend";
                }
    return relationship;
}

}

最后回到动作监听器,我实现了这个类:

        Relation relationship = new Relation(relation1, relation2, relation3
                , relation4);

        String arrayRelation = relationship.relations();

我最后将arrayRelation 包含在一个数组中,但该数组工作正常。

我的问题是我的 RadioButtons 的数组输出一直读取“null”(最有可能是因为此代码:String relationship = null;)。我认为这意味着我的 if else 语句都不满足,我真的不知道为什么。同样重要的是要指出,如果我单击提交而不单击任何单选按钮(按钮保持在“家庭”上),它会显示为空。如果我单击一个按钮,它可以完美地读取我想要的字符串。但是,如果我之后单击另一个按钮并再次单击提交,则字符串将返回为“null”。

我知道它很长,但我真的很感激任何帮助,因为我迷路了。

PS我的代码的某些部分是重复的,因为我正在尝试解决问题。

4

2 回答 2

1

我建议你单独处理你的动作事件,例如:

family.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            familyActionPerformed(evt);
        }
    });

然后实现familyActionPerformed(evt):

private void familyActionPerformed(java.awt.event.ActionEvent evt) {
    // every click on family radio button causes the code here to be executed
    relation1 = true;
}

还要为您单击的按钮编写一个事件处理程序,如下所示:

submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // Here test the state of each radio button
    relation1 = family.isSelected();
    relation2 = friend.isSelected();
    relation3 = relative.isSelected();
    relation4 = friendFriend.isSelected();
}

更多编辑:用 NetBeans 做你正在做的事情应该很容易。以下教程将为您解决所有问题:

  1. 教程 1

  2. 教程 2


我再次解释解决方案:

以“家庭”按钮为例,在您创建和初始化 GUI 组件的构造函数中执行以下操作:

JRadioButton family = new JRadioButton();
// do any other thing you want to do to this button and finally..
family.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            familyActionPerformed(evt);
        }
    });

JButton submit = new JButton("Submit");
submit.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            submitActionPerformed(evt);
        }
    });

然后在某处创建这些方法:

private void familyActionPerformed(java.awt.event.ActionEvent evt){
    // each time family is selected, you code processes the lines below:
    ...
}

private void submiteActionPerformed(java.awt.event.ActionEvent evt){
    relation1 = family.isSelected();
    relation2 = friend.isSelected();
    relation3 = relative.isSelected();
    relation4 = friendFriend.isSelected();
}

对其余的 RadioButtons 执行类似的操作。

于 2012-12-09T04:01:02.817 回答
1

我认为你让事情对自己来说太复杂了。如果您想要的只是按下的 JRadioButton 的字符串,则使用 ButtonGroup 为您获取它。它可以返回所选 JRadioButton 的 ButtonModel(如果选择了任何一个),然后您可以从中提取 actionCommand 字符串,尽管您必须记住在创建 JRadioButton 时设置它。

例如:

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

@SuppressWarnings("serial")
public class JRadioExample extends JPanel {
   private static final String[] RADIO_TITLES = { "Family", "Friend",
         "Relative", "Friend or Relative" };
   private ButtonGroup btnGrp = new ButtonGroup();

   public JRadioExample() {
      for (int i = 0; i < RADIO_TITLES.length; i++) {
         JRadioButton rBtn = new JRadioButton(RADIO_TITLES[i]);
         rBtn.setActionCommand(RADIO_TITLES[i]); // ***** this is what needs to
                                                 // be set
         btnGrp.add(rBtn);
         add(rBtn);
      }

      add(new JButton(new BtnAction("Get Chosen Selection")));
   }

   private class BtnAction extends AbstractAction {

      public BtnAction(String name) {
         super(name);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         ButtonModel model = btnGrp.getSelection();
         if (model != null) {
            String actionCommand = model.getActionCommand();
            System.out.println("Selected Button: " + actionCommand);
         } else {
            System.out.println("No Button Selected");
         }    
      }
   }

   private static void createAndShowGui() {
      JRadioExample mainPanel = new JRadioExample();

      JFrame frame = new JFrame("JRadioExample");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
于 2012-12-09T04:05:47.610 回答