首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BorderPane重叠内容

BorderPane重叠内容
EN

Stack Overflow用户
提问于 2019-09-07 03:11:24
回答 1查看 62关注 0票数 1

我有孩子的GridPane,我想如果文本会增加,那么文本不应该走出gridPane,而是ScrollPane,我不想看到go苹果文本开箱即用。

代码语言:javascript
复制
  @Override
public void start(Stage primaryStage) throws Exception {

    String border = "-fx-border-color:red;";

    Text title=new Text("Machin");

    GridPane topLeft=new GridPane();
    topLeft.setMinSize(80, 5);
    topLeft.setMaxSize(80, 80);

    topLeft.setStyle(border);

    topLeft.add(title, 0, 0);

    for(int i=1;i<=15;i++) {
        topLeft.add(new Text("Apple"), 0,i);
    }


    GridPane root = new GridPane();
    root.setAlignment(Pos.CENTER);

    root.setHgap(20);
    root.setVgap(20);

    root.add(topLeft, 0, 0);



    BorderPane borderPane = new BorderPane();
    borderPane.setTop(new Label("Header"));
    borderPane.setCenter(root);
    borderPane.setBottom(new Label("Buttom"));


    primaryStage.setScene(new Scene(borderPane, 600, 400));
    primaryStage.show();
}

图片:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-07 06:46:18

您可以使用一些更复杂的控件(如ListViewTableView )来封装您的内容,而不是像GridPane那样的布局窗格。这些控件有内置的滚动条,当控件的内容大于可视区域时,滚动条会显示出来。查看链接的教程以评估这些其他控件是否适用于您的应用程序。

如果您希望继续使用GridPane,则可以将GridPane封装在ScrollPane中,以便在GridPane的内容达到一定大小时滚动它,如下例所示:

代码语言:javascript
复制
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class ScrollingGrid extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        String border = "-fx-border-color:red;";
        Text title = new Text("Machin");

        GridPane appleGrid = new GridPane();
        appleGrid.setMinSize(80, 5);
        appleGrid.setStyle(border);
        appleGrid.add(title, 0, 0);

        for (int i = 1; i <= 15; i++) {
            appleGrid.add(new Text("Apple"), 0, i);
        }

        ScrollPane scrollPane = new ScrollPane(appleGrid);
        scrollPane.setPrefViewportWidth(80);
        scrollPane.setPrefViewportHeight(80);
        scrollPane.setMaxSize(ScrollPane.USE_PREF_SIZE, ScrollPane.USE_PREF_SIZE);

        BorderPane borderPane = new BorderPane();
        borderPane.setTop(new Label("Header"));
        borderPane.setCenter(scrollPane);
        borderPane.setBottom(new Label("Bottom"));

        primaryStage.setScene(new Scene(borderPane, 600, 400));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57827404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档