无线电按钮和布尔连接的麻烦

时间:2012-12-09 03:54:53

标签: java swing if-statement boolean jradiobutton

好吧很奇怪。顺便说一句,我对radiobutton并不擅长。但我在netbeans中制作了一个包含RadioButton的JPanel程序。你用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;

然后我创建了一个关系类: 公共类关系{     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。如果我单击一个按钮就可以完美地读取我想要的字符串。但是,如果我之后单击另一个按钮并再次单击“提交”,则字符串将返回“null”。

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

P.S。我的代码的某些部分是重复的,因为我正在试图解决问题。

2 个答案:

答案 0 :(得分: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. Tutorial 1

  2. Tutorial 2


  3. 我再次解释解决方案:

    使用'family'按钮作为示例,在您已创建并初始化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做类似的事情。

答案 1 :(得分:1)

我认为你让事情变得过于复杂。如果您想要的是按下的JRadioButton的String,那么使用ButtonGroup为您获取它。它可以返回所选JRadioButton的ButtonModel(如果选择了任何一个),你可以从中提取actionCommand String,尽管你必须记住在创建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();
         }
      });
   }
}