Java – libgdx and scrollpane with multiple widgets

Trying to add multiple items to the scroll bar, I soon found that all "addactor" functions were not supported So, I added a table, all the items I want (this code missed the image I still want to add) to create a scrollable credit screen... But this method (currently) does not allow overflow, rendering scrollpane is useless (my text is only displayed to the position allowed by the screen height, and cannot be scrolled)

pane = new ScrollPane(null,skin);
    pane.setFillParent(true);
    paneContent = new Table(skin);
    paneContent.setFillParent(true);
    Label temp = new Label("",skin);
    temp.setAlignment(Align.left,Align.center);
    temp.setText( Gdx.files.internal("licenses/credits.txt").readString("UTF-8") );
    paneContent.addActor(temp);
    pane.setWidget(paneContent);
    stage.addActor(pane);

Solution

If you want to put multiple items into the scrollpane, you just need to put a table in it and call add () for each widget you want to put into the scrollpane

Here is an example of how to make your credit scrollable:

public class ScrollTest implements ApplicationListener {
    private Stage stage;

    private static final String reallyLongString = "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n"
        + "This\nIs\nA\nReally\nLong\nString\nThat\nHas\nLots\nOf\nLines\nAnd\nRepeats.\n";

    @Override public void create() {
        this.stage = new Stage();
        Gdx.input.setInputProcessor(this.stage);
        final Skin skin = new Skin(Gdx.files.internal("skin/uiskin.json"));

        final Label text = new Label(reallyLongString,skin);
        text.setAlignment(Align.center);
        text.setWrap(true);
        final Label text2 = new Label("This is a short string!",skin);
        text2.setAlignment(Align.center);
        text2.setWrap(true);
        final Label text3 = new Label(reallyLongString,skin);
        text3.setAlignment(Align.center);
        text3.setWrap(true);

        final Table scrollTable = new Table();
        scrollTable.add(text);
        scrollTable.row();
        scrollTable.add(text2);
        scrollTable.row();
        scrollTable.add(text3);

        final ScrollPane scroller = new ScrollPane(scrollTable);

        final Table table = new Table();
        table.setFillParent(true);
        table.add(scroller).fill().expand();

        this.stage.addActor(table);
    }

    @Override public void render() {
        this.stage.act();
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        this.stage.draw();
    }

    @Override public void resize(final int width,final int height) {}
    @Override public void pause() {}
    @Override public void resume() {}
    @Override public void dispose() {}
}

Edit: added code to set the table in scrollpane

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>