|
| 1 | +package basico; |
| 2 | + |
| 3 | +import javafx.application.Application; |
| 4 | +import javafx.geometry.Pos; |
| 5 | +import javafx.scene.Scene; |
| 6 | +import javafx.scene.control.Button; |
| 7 | +import javafx.scene.control.Label; |
| 8 | +import javafx.scene.layout.HBox; |
| 9 | +import javafx.scene.layout.VBox; |
| 10 | +import javafx.stage.Stage; |
| 11 | + |
| 12 | +public class Contador extends Application { |
| 13 | + |
| 14 | + private int contador = 0; |
| 15 | + |
| 16 | + private void atualizarLabelNumero(Label label) { |
| 17 | + label.setText(Integer.toString(contador)); |
| 18 | + |
| 19 | + label.getStyleClass().remove("verde"); |
| 20 | + label.getStyleClass().remove("vermelho"); |
| 21 | + |
| 22 | + if(contador > 0) { |
| 23 | + label.getStyleClass().add("verde"); |
| 24 | + } else if(contador < 0) { |
| 25 | + label.getStyleClass().add("vermelho"); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void start(Stage primaryStage) throws Exception { |
| 31 | + |
| 32 | + Label labelTitulo = new Label("Contador"); |
| 33 | + labelTitulo.getStyleClass().add("titulo"); |
| 34 | + |
| 35 | + Label labelNumero = new Label("0"); |
| 36 | + labelNumero.getStyleClass().add("numero"); |
| 37 | + |
| 38 | + Button botaoDecremento = new Button("-"); |
| 39 | + botaoDecremento.getStyleClass().add("botoes"); |
| 40 | + botaoDecremento.setOnAction(e -> { |
| 41 | + contador--; |
| 42 | + atualizarLabelNumero(labelNumero); |
| 43 | + }); |
| 44 | + |
| 45 | + Button botaoIncremento = new Button("+"); |
| 46 | + botaoIncremento.getStyleClass().add("botoes"); |
| 47 | + botaoIncremento.setOnAction(e -> { |
| 48 | + contador++; |
| 49 | + atualizarLabelNumero(labelNumero); |
| 50 | + }); |
| 51 | + |
| 52 | + HBox boxBotoes = new HBox(); |
| 53 | + boxBotoes.setAlignment(Pos.CENTER); |
| 54 | + boxBotoes.setSpacing(10); |
| 55 | + boxBotoes.getChildren().add(botaoDecremento); |
| 56 | + boxBotoes.getChildren().add(botaoIncremento); |
| 57 | + |
| 58 | + VBox boxConteudo = new VBox(); |
| 59 | + boxConteudo.getStyleClass().add("conteudo"); |
| 60 | + boxConteudo.setSpacing(10); |
| 61 | + boxConteudo.setAlignment(Pos.CENTER); |
| 62 | + boxConteudo.getChildren().add(labelTitulo); |
| 63 | + boxConteudo.getChildren().add(labelNumero); |
| 64 | + boxConteudo.getChildren().add(boxBotoes); |
| 65 | + |
| 66 | + String caminhoDoCss = getClass() |
| 67 | + .getResource("/basico/Contador.css").toExternalForm(); |
| 68 | + |
| 69 | + Scene cenaPrincipal = new Scene(boxConteudo, 400, 400); |
| 70 | + cenaPrincipal.getStylesheets().add(caminhoDoCss); |
| 71 | + cenaPrincipal.getStylesheets().add("https://fonts.googleapis.com/css?family=Oswald"); |
| 72 | + |
| 73 | + primaryStage.setScene(cenaPrincipal); |
| 74 | + primaryStage.show(); |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + launch(args); |
| 79 | + } |
| 80 | +} |
0 commit comments