日期:2014-05-20  浏览次数:20757 次

javaFX中的Stage窗口怎么最大化、最小化?
javaFX中的Stage窗口怎么使用程序而不是用户操作界面最大化/最小化?
stage.getEventDispatcher().dispatchEvent();事件类型没找到最大化/最小化的事件!

------解决方案--------------------
不过还是给你做了个。。
/**
 * Define a "Close" button, which is indispensable for an undecorated stage
 * 
 * @return a "Close" button
 */
private Node createCloseButton() {
closeButton = new Button("Close");
closeButton.setId("button-close");
closeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Platform.exit();
}
});

return closeButton;
}

/**
 * Define a "Maximize" button
 * 
 * @return a "Maximize" button
 */
private Node createMaximizeButton() {
Button maximizeButton = new Button("Maximize");
maximizeButton.setId("button-maximize");

maximizeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
/* Toggle the maximizing action */
toggleMaximized(applicationStage);
}
});

return maximizeButton;
}

/**
 * Toggle the maximized action.
 * 
 * @param primaryStage
 *            - the primary stage
 */
private void toggleMaximized(Stage primaryStage) {
ObservableList<Screen> screens = Screen.getScreensForRectangle(primaryStage.getX(), primaryStage.getY(), 1, 1);
if (screens.isEmpty()) {
screen = Screen.getScreensForRectangle(0, 0, 1, 1).get(0);
} else {
screen = screens.get(0);
}

if (maximized.get()) {
maximized.set(false);
if (backupWindowBounds != null) {
minimizeButton.setStyle("-fx-background-insets: 0 0 -1 0, 0, 1, 2; " + "-fx-background-color: -fx-shadow-highlight-color, "
+ "-fx-outer-border, -fx-inner-border, -fx-body-color;");
closeButton.setStyle("-fx-background-insets: 0 0 -1 0, 0, 1, 2;" + "-fx-background-color: -fx-shadow-highlight-color, "
+ "-fx-outer-border, -fx-inner-border, -fx-body-color;");
primaryStage.setX(backupWindowBounds.get().getMinX());
primaryStage.setY(backupWindowBounds.get().getMinY());
primaryStage.setWidth(backupWindowBounds.get().getWidth());
primaryStage.setHeight(backupWindowBounds.get().getHeight());
}
} else {
maximized.set(true);
minimizeButton.setStyle("-fx-background-insets: -1.4, 0, 1, 2; " + "-fx-background-color: transparent, -fx-outer-border, "
+ "-fx-inner-border, -fx-body-color;");
closeButton.setStyle("-fx-background-insets: -1.4, 0, 1, 2; " + "-fx-background-color: transparent, -fx-outer-border, "
+ "-fx-inner-border, -fx-body-color;");
backupWindowBounds.set(new Rectangle2D(primaryStage.getX(), primaryStage.getY(), primaryStage.getWidth(), primaryStage.getHeight()));
primaryStage.setX(screen.getVisualBounds().getMinX() - 20);
primaryStage.setY(screen.getVisualBounds().getMinY() - 10);
primaryStage.setWidth(screen.getVisualBounds().getWidth() + 40);
primaryStage.setHeight(screen.getVisualBounds().getHeight() + 30);
}
}

/**
 * Define a "Minimize" button
 * 
 * @return a "Minimize" button
 */
private Node createMinimizeButton() {
minimizeButton = new Button("Minimize");
minimizeButton.setId("button-minimize");
minimizeButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
applicationStage.setIconified(true);
}
});

return minimizeButton;
}

/**
 * Enable the root node to be dragged. Realize some drag functions.
 * 
 * @param root
 *            - the root node