Skip to content

#08.1 Button

Valkryst edited this page Apr 24, 2018 · 14 revisions
import com.valkryst.VTerminal.Screen;
import com.valkryst.VTerminal.builder.ButtonBuilder;

import java.io.IOException;

public class Driver {
    public static void main(final String[] args) throws IOException {
        final Screen screen = new Screen();
        screen.addCanvasToFrame();

        final ButtonBuilder buttonBuilder = new ButtonBuilder();
        buttonBuilder.setPosition(10, 10);
        buttonBuilder.setText("Click Me");
        buttonBuilder.setOnClickFunction(() -> System.out.println("You clicked a button."));

        screen.addComponent(buttonBuilder.build());
        screen.draw();
    }
}

Code Explanation

final ButtonBuilder buttonBuilder = new ButtonBuilder();

Constructs a new ButtonBuilder.

You can reuse the builder, so you won't need to create a new ButtonBuilder every time you want to create a new button.


buttonBuilder.setPosition(10, 10);

This tells the builder to place the button at position (10x, 10y).


buttonBuilder.setText("Click Me");

This sets the text on the button, so this button will display "Click Me".


buttonBuilder.setOnClickFunction(() -> System.out.println("You clicked a button."));

This is an optional setter which gives the button a function to run when it's clicked.

In this case, the function will print "You clicked a button." when the button is pressed.


screen.addComponent(buttonBuilder.build());

This builds the button and adds it to the screen.


screen.draw();

This redraws the screen, to show the newly added button.

Result