【发布时间】:2017-04-30 16:24:54
【问题描述】:
我对 JavaFX 有疑问。
我想在 Dialog 中放置一个 ScrollPane 并给 Scrollpane 一个最大高度,没有最小高度或预置高度,因为它可以更短。
这是一个简单的例子来演示我的问题。
主类:
package application;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application
{
@Override
public void start(Stage primaryStage)
{
try
{
StackPane root = new StackPane();
MyDialog dialog = new MyDialog();
Button button = new Button("Test");
button.setOnMousePressed(e -> dialog.showAndWait());
root.getChildren()
.add(button);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
launch(args);
}
}
对话框类:
package application;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.control.DialogPane;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
public class MyDialog extends Dialog<ButtonType>
{
public MyDialog()
{
super();
DialogPane dialogPane = new DialogPane();
VBox outer = new VBox();
Button button = new Button("Test");
setResult(new ButtonType("NO", ButtonData.LEFT));
button.setOnMousePressed(e -> close());
VBox inner = new VBox();
inner.getChildren()
.add(new Text("Test 1"));
inner.getChildren()
.add(new Text("Test 2"));
inner.getChildren()
.add(new Text("Test 3"));
inner.getChildren()
.add(new Text("Test 4"));
inner.getChildren()
.add(new Text("Test 5"));
ScrollPane scroll = new ScrollPane(inner);
scroll.setMaxHeight(80);
outer.getChildren()
.addAll(button, scroll);
dialogPane.setContent(outer);
this.setDialogPane(dialogPane);
}
}
我不知道是什么问题。希望有人能告诉我为什么这不起作用。
【问题讨论】:
-
你好,我找到了一个不太好的解决这个问题的方法。我覆盖了 ScrollPane 的 computeMinHeight(double width) 方法并返回内容的高度(或 ScrollPane 的最大高度)。但我想也许有一个更漂亮的解决方案。