【问题标题】:JavaFX - Dealing with duplicate children error - RadioButtonsJavaFX - 处理重复的子错误 - RadioButtons
【发布时间】:2014-12-10 17:39:11
【问题描述】:

我有三个单选按钮,我希望根据我单击的复选框上传我的 GUI。

我曾参考过本指南:http://docs.oracle.com/javafx/2/ui_controls/radio-button.htm 这似乎很适合我的需要。但是,尽管导入了正确的库,它仍无法识别类型。我决定编写自己的小代码。

我们有水平框和垂直框。我已将标签和文本元素添加到一个水平框中(由 hb、hb1、hb2.. 表示)并将它们全部组合在一个垂直框中。

这是我的单选按钮事件监听器代码:

test1.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {

        try{
            //Remove all existing children
            vb.getChildren().removeAll(hb,hb2,hb3,hb4,hb5);
            log("Cleared");
        } catch(Exception e)
        {
            log(e.getMessage());
        }
        try{
            //Add email and email text into a horizontal box
            hb.getChildren().addAll(email, email_text);
            hb.setSpacing(10);
            //Added the box to the vertical box
            vb.getChildren().add(hb);

            log("Added the email input box");
        }
        catch(Exception e){
            log(e.getMessage());
        }
    }
});

声明:

final ToggleGroup group = new ToggleGroup();

final RadioButton test1 = new RadioButton("Test 1");
test1.setToggleGroup(group);
grid.add(test1, 0, 1);

final RadioButton test2 = new RadioButton("Test 2");
test2.setToggleGroup(group);
grid.add(test2, 0, 2);

final  VBox vb = new VBox(10); // main container
final HBox hb = new HBox(); // Email , Email Text
final HBox hb2 = new HBox(); // Corporate , Corporate Name

我收到一个错误:Children: duplicate children added: parent = HBox@359889fd。我对此做了一些研究:java.lang.IllegalArgumentException: Children: duplicate children added: parent = VBox@872be7。老实说,即使我们都在处理同一个问题,我也很难得到他的代码。

我的主要问题:如果我一开始就删除所有元素,为什么会出现重复错误?

也许,我的代码可能完全错误并且毫无意义。您是否推荐任何其他方法?

【问题讨论】:

  • 添加到hb 时可能会出错。从vb 中删除时,也从hb 中删除
  • 嘿! Vb 包含所有的 hb 元素。如果我删除了 Vb 的孩子……不应该删除 hb 吗?
  • 添加到hb时是否有错误? (第二次尝试捕获)。如果是,这意味着您已经有一些元素,请在添加(电子邮件,电子邮件文本)之前尝试删除它们(重复)。现在您只从vb 中删除。你必须具体说明你在哪里得到这个错误。
  • 第一次单击任何复选框时,我得到“已清除添加了电子邮件输入框”第二次单击任何复选框时,我得到“已清除”,然后是“孩子:添加了重复的孩子:父母 = HBox@359889fd"
  • 问题是,只要我点击任何复选框(第一个复选框),我就会删除它们,但它说它是重复的

标签: java javafx


【解决方案1】:

主要问题是我删除了 vb 框,但没有删除 hb 的内容。然而,我正在添加 hb。我也应该从 hb 中删除。

【讨论】:

  • 是的!我希望您将其发布为答案!
猜你喜欢
  • 2016-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
相关资源
最近更新 更多