17 de nov. de 2015

Rapidinha: Dimensionar Stage

e Olá Pessoal! Inaugurando hoje o "rapidinhas", onde eu posto exemplos rápidos de código. Hoje eu vi essa postagem no JavaFree e quis criar um exemplo rapidinho. Assim, fiz o programinha abaixo que permite manipular um Stage (tamanho, maximizar e fechar). Um gist está incorporado abaixo, mas também coloquei no github.


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class DimensionarStage extends Application {
private static final double MAX_ALTURA = 500;
private static final double MAX_LARGURA = 600;
private static final double MIN_LARGURA = 350;
private static final double MIN_ALTURA = 70;
private static final double PASSO = 10;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage stage) throws Exception {
Button btnAumentar = new Button("Aumentar");
Button btnDim = new Button("Diminuir");
Button btnMax = new Button("Maximizar");
Button btnFechar = new Button("Fechar");
HBox hbBotoes = new HBox(btnAumentar, btnDim, btnMax, btnFechar);
hbBotoes.setSpacing(20);
// permite trabalhar com os tamanhos do Stage
btnAumentar.setOnAction(e -> {
double w = stage.getWidth();
double h = stage.getHeight();
if (w < MAX_LARGURA) {
stage.setWidth(w + PASSO);
}
if (h < MAX_ALTURA) {
stage.setHeight(h + PASSO);
}
});
btnDim.setOnAction(e -> {
double w = stage.getWidth();
double h = stage.getHeight();
if (w > MIN_LARGURA) {
stage.setWidth(w - PASSO);
}
if (h > MIN_ALTURA) {
stage.setHeight(h - PASSO);
}
});
btnMax.setOnAction(e -> stage.setMaximized(true));
btnFechar.setOnAction(e -> stage.close());
// Desabilita se o Stage estiver maximizado
btnMax.disableProperty().bind(stage.maximizedProperty());
stage.setTitle("Dimensionando o Stage");
stage.setScene(new Scene(hbBotoes));
stage.show();
}
}

2 comentários:

  1. Este comentário foi removido pelo autor.

    ResponderExcluir
  2. Como faço para fechar a tela de login e ficar somente com a tela principal e tb para fechar as telas dentro da aplicação.
    Antes saia da app com exit/close e fechava com dispose, mas agora não sei.

    ResponderExcluir