1

在这种情况下,根布局具有内容...

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 时,我试图让它在根布局中添加一个按钮...

4

1 回答 1

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 是一个低流量标签)

于 2015-05-06T13:22:56.750 回答