如何在布局成为场景的一部分后添加组件?

时间:2014-12-17 18:32:57

标签: scalafx

这种情况下的根布局有内容......

object SomeApp extends JFXApp {

  stage = new PrimaryStage {
    title = "SomeApp"
    width = 800
    height = 600

    val TheButton = new Button("Add a Button") {
      onAction = {
        e: ActionEvent => {
          root.dd(new Button("Some Button"))
        }
      }
    }

    val SomeLayout = new AnchorPane {
      styleClass += "someStyle"

      content = TheButton
    }

    scene = new Scene {
      stylesheets = List(getClass.getResource("Extend.css").toExternalForm)

      root = new AnchorPane {

        content = SomeLayout
      }
    }
  }
}

我试图让它在单击TheButton时向根布局添加一个按钮...

1 个答案:

答案 0 :(得分:0)

这有效:

object SFXAddElemAfterStart extends JFXApp {

   stage = new PrimaryStage {
      title = "SFXAddElemAfterStart"
      width = 800
      height = 600


      scene = new Scene {

         val paneNeedingButton = new AnchorPane {
            content = layout
         }
         root = paneNeedingButton

         def layout: AnchorPane = new AnchorPane {
            styleClass += "someStyle"

            content = button
         }

         def button = new Button("Add a Button") {
            onAction = {
               e: ActionEvent => {
                  paneNeedingButton.children += new Button("Some Button")
               }
            }
         }
      }
   }
}

您只需将按钮放在合理的位置即可。

(顺便说一句,对于漫长的等待,scalafx是一个低流量标签)