如何在 JavaFX 中创建可滚动的组件面板?
How to create a scrollable panel of components in JavaFX?
所以,基本上,我正在尝试实现一组具有间距等的可滚动组件以及添加更多组件的能力。
组件列表是自定义锚定窗格组件。
这是我工作的一个例子:
问题在于,它在滚动窗格内部使用了网格窗格,并且出于某种原因,网格窗格根本没有填满滚动窗格的宽度。因此,如果我调整大小,所有内容都会保留在原处,而不是在网格窗格列内拉伸。另外,如果我坚持使用网格窗格(在一个似乎工作得更好的瓷砖窗格上,除非组件彼此相邻浮动,尽管方向是垂直的并且 prefcolumns 是 1),我将如何添加更多行?这样做的想法是以漂亮、令人愉悦的时尚方式列出 github 回购协议。
感谢您的任何见解。
另外:这些回购协议不是我的 - 我有 none 并且网站上的一个人制作了它(它制作得不好所以不要将我与他相比哈哈)。
这看起来你应该使用 VBox
来容纳你的 AnchorPane
并将它们放在 ScrollPane
中。在 ScrollPane
上将 fitToWidth
设置为 true
并且它应该强制锚定窗格占据滚动窗格视口的整个宽度(并且不会更多),假设您不更改maxWidth
属性 上的 AnchorPane
默认值。
模拟示例(如果您愿意,可以在 FXML 中轻松完成):
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ScrollingVBox extends Application {
@Override
public void start(Stage primaryStage) {
final Random rng = new Random();
VBox content = new VBox(5);
ScrollPane scroller = new ScrollPane(content);
scroller.setFitToWidth(true);
Button addButton = new Button("Add");
addButton.setOnAction(e -> {
AnchorPane anchorPane = new AnchorPane();
String style = String.format("-fx-background: rgb(%d, %d, %d);"+
"-fx-background-color: -fx-background;",
rng.nextInt(256),
rng.nextInt(256),
rng.nextInt(256));
anchorPane.setStyle(style);
Label label = new Label("Pane "+(content.getChildren().size()+1));
AnchorPane.setLeftAnchor(label, 5.0);
AnchorPane.setTopAnchor(label, 5.0);
Button button = new Button("Remove");
button.setOnAction(evt -> content.getChildren().remove(anchorPane));
AnchorPane.setRightAnchor(button, 5.0);
AnchorPane.setTopAnchor(button, 5.0);
AnchorPane.setBottomAnchor(button, 5.0);
anchorPane.getChildren().addAll(label, button);
content.getChildren().add(anchorPane);
});
Scene scene = new Scene(new BorderPane(scroller, null, null, addButton, null), 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
另一个解决方案可能是使用 ListView
。 (如果你有很多项目要显示,这会更好。)创建一个 class 代表你在每个 AnchorPane
中显示的项目和一个自定义列表单元格来显示它们。
所以,基本上,我正在尝试实现一组具有间距等的可滚动组件以及添加更多组件的能力。 组件列表是自定义锚定窗格组件。
这是我工作的一个例子:
问题在于,它在滚动窗格内部使用了网格窗格,并且出于某种原因,网格窗格根本没有填满滚动窗格的宽度。因此,如果我调整大小,所有内容都会保留在原处,而不是在网格窗格列内拉伸。另外,如果我坚持使用网格窗格(在一个似乎工作得更好的瓷砖窗格上,除非组件彼此相邻浮动,尽管方向是垂直的并且 prefcolumns 是 1),我将如何添加更多行?这样做的想法是以漂亮、令人愉悦的时尚方式列出 github 回购协议。
感谢您的任何见解。 另外:这些回购协议不是我的 - 我有 none 并且网站上的一个人制作了它(它制作得不好所以不要将我与他相比哈哈)。
这看起来你应该使用 VBox
来容纳你的 AnchorPane
并将它们放在 ScrollPane
中。在 ScrollPane
上将 fitToWidth
设置为 true
并且它应该强制锚定窗格占据滚动窗格视口的整个宽度(并且不会更多),假设您不更改maxWidth
属性 上的 AnchorPane
默认值。
模拟示例(如果您愿意,可以在 FXML 中轻松完成):
import java.util.Random;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class ScrollingVBox extends Application {
@Override
public void start(Stage primaryStage) {
final Random rng = new Random();
VBox content = new VBox(5);
ScrollPane scroller = new ScrollPane(content);
scroller.setFitToWidth(true);
Button addButton = new Button("Add");
addButton.setOnAction(e -> {
AnchorPane anchorPane = new AnchorPane();
String style = String.format("-fx-background: rgb(%d, %d, %d);"+
"-fx-background-color: -fx-background;",
rng.nextInt(256),
rng.nextInt(256),
rng.nextInt(256));
anchorPane.setStyle(style);
Label label = new Label("Pane "+(content.getChildren().size()+1));
AnchorPane.setLeftAnchor(label, 5.0);
AnchorPane.setTopAnchor(label, 5.0);
Button button = new Button("Remove");
button.setOnAction(evt -> content.getChildren().remove(anchorPane));
AnchorPane.setRightAnchor(button, 5.0);
AnchorPane.setTopAnchor(button, 5.0);
AnchorPane.setBottomAnchor(button, 5.0);
anchorPane.getChildren().addAll(label, button);
content.getChildren().add(anchorPane);
});
Scene scene = new Scene(new BorderPane(scroller, null, null, addButton, null), 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
另一个解决方案可能是使用 ListView
。 (如果你有很多项目要显示,这会更好。)创建一个 class 代表你在每个 AnchorPane
中显示的项目和一个自定义列表单元格来显示它们。