在我的ScalaFX项目中,我想为用户创建一个单独的帮助页面,在该页面中,他/她可以单击一个主题,从而导致在可单击主题旁边显示说明.
问题是,当我为帮助页面创建新场景时,无论做什么,我都无法移动TextFlow.它位于ListView顶部的同一位置.请参阅图片以供参考.
helpMenu.onAction = (ae: ActionEvent) => {
val helpStage = new Stage()
helpStage.setTitle("A wild help stage appears!")
val helpScene = new Scene(scene.width.get * 0.6,scene.height.get * 0.8) {
val listView = new ListView(List.tabulate(3) { n => "Option " + (n + 1) })
val text1 = new Text("This is fine\n*zips coffee*\nThis is all fine.")
val textFlow = new TextFlow(text1)
/*
* Nothing below changes where the text appears in the new scene.
*/
textFlow.layoutX <== listView.width.get
textFlow.alignmentInParent_=(Pos.TOP_RIGHT)
textFlow.alignmentInParent_=(Pos.BASELINE_CENTER)
text1.alignmentInParent_=(Pos.BASELINE_CENTER)
listView.prefHeight <== scene.height.get * 0.4
content.addAll(listView,textFlow)
}
helpStage.setScene(helpScene)
helpStage.show()
helpScene.listView.onMouseClicked = (le: MouseEvent) => {
val item = helpScene.listView.selectionModel.apply.getSelectedItem
}
}
我想知道的是如何定位列表视图旁边的文本?目的是使说明出现在此处.我的计划是使用
helpScene.listView.onMouseClicked =(le:MouseEvent)=> {
val item = helpScene.listView.selectionModel.apply.getSelectedItem
}
为此,因为我们可以轻松确定列表中单击的内容.
最佳答案
我认为这里的问题是您需要将ListView和TextFlow元素添加到一个容器中,以控制它们的布局方式.在下面的代码中,我为此目的使用了HBox,它将两个并排放置. (对于较旧的JavaFX 2版本,scalafx.scene.layout包中还有其他选项,例如VBox,BorderPane等.This tutorial,可能有助于您更好地理解该主题.)
原文链接:https://www.f2er.com/java/532875.html之后,我不清楚您要实现的目标,但是我认为您要尝试的是根据选择ListView中的哪个项目在TextFlow中显示不同的文本.如果真是这样,那么以下示例通过使用选择更改来触发TextFlow内容更改来解决此问题. (如果您不是这样想的,您可以详细说明您的要求吗?)
另外,我还简化了一些代码以使用ScalaFX的属性(与JavaFX的getter和setter相对).
onAction = (ae: ActionEvent) => {
val helpStage = new Stage() {
// Make room for the list view and the text flow.
width = 400
// Set the help text to the default.
val defaultText = "Please make\na selection"
val helpText = new Text(defaultText)
// Help for each option in the list view. If a key doesn't match,then the default
// text is displayed.
val optionText = Map(
"Option 1" -> "This is\nsome help for\nOption 1","Option 2" -> "And this is\nsome help for\nOption 2","Option 3" -> "Finally,this is\nsome help for\nOption 3",).withDefaultValue(defaultText)
// Title the stage and set the scene...
title = "A wild help stage appears!"
scene = new Scene {
val listView = new ListView(List.tabulate(3) { n => "Option " + (n + 1) }) {
// Change the help text when the selection changes.
selectionModel().selectedItem.onChange {(_,_,selected) =>
helpText.text = optionText(selected);
}
}
// Add the help text to the text flow.
val textFlow = new TextFlow(helpText)
// Put list view and text flow elements side-by-side.
content = new HBox(listView,textFlow)
}
}
helpStage.show()
}