ToggleGroup用于在Java中在男性和女性之间切换

huangapple go评论71阅读模式
英文:

ToggleGroup to toggle between male and female java

问题

我正在尝试创建一个切换按钮用于更改所选的性别我试图让它在男性和女性之间切换但是当我运行它并选择一个组时它既不选择男性也不选择女性

ToggleGroup genderToggle = new ToggleGroup();
maleButton.setToggleGroup(genderToggle);
maleButton.setUserData("male");
femaleButton.setToggleGroup(genderToggle);
femaleButton.setUserData("female");
submitButton.setOnAction(new ButtonListener());
genderToggle.selectedToggleProperty().addListener(new ChangeListener<Toggle>()  
{ 
    public void changed(ObservableValue<? extends Toggle> ob, Toggle o, Toggle n) 
    {
        RadioButton rb = (RadioButton)genderToggle.getSelectedToggle(); 

        if (rb.getUserData().equals("male"))
        {
            maleButton.setSelected(true);
            femaleButton.setSelected(false);
        }
        else if (rb.getUserData().equals("female"))
        {
            maleButton.setSelected(false);
            femaleButton.setSelected(true);
        }
        else
        {
            maleButton.setSelected(false);
            femaleButton.setSelected(false);
        }
    } 
});
英文:

I am attempting to create a toggle button that changes the selected gender, I am trying to get it to toggle between male and female but when I run it and select a group, it doesn't select either one.

	ToggleGroup genderToggle = new ToggleGroup();
	maleButton.setToggleGroup(genderToggle); 
	maleButton.setUserData(&quot;male&quot;);
	femaleButton.setToggleGroup(genderToggle); 
	femaleButton.setUserData(&quot;female&quot;);
	submitButton.setOnAction(new ButtonListener());
    genderToggle.selectedToggleProperty().addListener(new ChangeListener&lt;Toggle&gt;()  
    { 
        public void changed(ObservableValue&lt;? extends Toggle&gt; ob, Toggle o, Toggle n) 
        {
            RadioButton rb = (RadioButton)genderToggle.getSelectedToggle(); 

		if (genderToggle.equals(&quot;male&quot;))
		{
			maleButton.setSelected(true);
			femaleButton.setSelected(false);
		}
		else if (genderToggle.equals(&quot;female&quot;))
		{
			maleButton.setSelected(false);
			femaleButton.setSelected(true);
		}
		else
		{
			maleButton.setSelected(false);
			femaleButton.setSelected(false);
		}
        } 
    });

答案1

得分: 1

我认为问题出在您在切换对象上使用了.equals(...),而应该首先获取用户数据字段,因为这是您设置为性别字符串的字段。例如:

if (genderToggle.getUserData().equals("female"))
{
    maleButton.setSelected(false);
    femaleButton.setSelected(true);
}
英文:

I think the issue is that you are using .equals(...) on the toggle object, instead first get the user data field, as this is the one you set to the gender string. E.g.:

if (genderToggle.getUserData().equals(&quot;female&quot;))
   {
      maleButton.setSelected(false);
      femaleButton.setSelected(true);
   }

huangapple
  • 本文由 发表于 2020年10月17日 17:41:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/64401072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定