1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package glasspane;
import java.net.URL;
import java.util.Optional;
import java.util.stream.IntStream;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
final StackPane root = new StackPane();
final Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(actionEvent -> showPopup(root));
root.getChildren().add(btn);
final Scene scene = new Scene(root, 300, 250);
final Optional<URL> cssUrl = Optional.ofNullable(getClass().getResource("glasspane.css"));
cssUrl.ifPresent(url -> scene.getStylesheets().add(url.toExternalForm()));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
private boolean isAnimated = true;
private void showPopup(final StackPane root) {
final Node popup = createPopup();
final Region glassPane = new Region();
glassPane.setId("glassPane");
glassPane.getStyleClass().add("glass-pane");
glassPane.setMouseTransparent(false);
glassPane.setOnMouseEntered(mouseEvent -> disposePopup(root, glassPane, popup));
root.getChildren().addAll(glassPane, popup);
if (isAnimated) {
final FadeTransition fadeIn = new FadeTransition(Duration.millis(650), popup);
fadeIn.setFromValue(0);
fadeIn.setToValue(1);
fadeIn.play();
}
}
private void disposePopup(final StackPane root, final Node glassPane, final Node popup) {
glassPane.setOnDragEntered(null);
if (isAnimated) {
final FadeTransition fadeIn = new FadeTransition(Duration.millis(650), popup);
fadeIn.setToValue(0);
fadeIn.setOnFinished(actionEvent -> root.getChildren().removeAll(glassPane, popup));
fadeIn.play();
} else {
root.getChildren().removeAll(glassPane, popup);
}
}
private Node createPopup() {
final VBox result = new VBox();
result.setId("popup");
result.getStyleClass().add("popup");
result.setMinSize(VBox.USE_PREF_SIZE, VBox.USE_PREF_SIZE);
result.setMaxSize(VBox.USE_PREF_SIZE, VBox.USE_PREF_SIZE);
result.getChildren()
.addAll(IntStream.range(0, 10)
.mapToObj(index -> String.format("Label %d", index + 1))
.map(Label::new)
.toArray(Label[]::new));
return result;
}
public static void main(String[] args) {
launch(args);
}
} |
Partager