不过还是给你做了个。。
/**
* 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
*/
private void enableDragging(final AnchorPane root) {
/*
* This mouse event is for resizing window. Define some specific cursor patterns.
*/
root.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
if (me.getX() > root.getWidth() - 10 && me.getX() < root.getWidth() + 10 && me.getY() > root.getHeight() - 10
&& me.getY() < root.getHeight() + 10) {
root.setCursor(Cursor.SE_RESIZE);
} else if (me.getX() > root.getWidth() - 5 && me.getX() < root.getWidth() + 5) {
root.setCursor(Cursor.H_RESIZE);
} else if (me.getY() > root.getHeight() - 5 && me.getY() < root.getHeight() + 5) {
root.setCursor(Cursor.V_RESIZE);
} else {
root.setCursor(Cursor.DEFAULT);
}
}
}); /* when mouse button is pressed, save the initial position of screen. */
root.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
Window primaryStage = root.getScene().getWindow();
anchor.set(new Point2D(me.getScreenX() - primaryStage.getX(), me.getScreenY() - primaryStage.getY()));
widthStage.set(primaryStage.getWidth());
heightStage.set(primaryStage.getHeight());
}
}); /*
* when mouse button is released, clear the initial position of screen. This action is very important, because it can be a judge to avoid
* mouse interaction with other nodes as ChoiceBox.
*/
root.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
anchor.set(null);
}
}); /*
* when screen is dragged, translate it accordingly. Note that the transparent pixels part contained in the root will not react to the drag.
* But translucent color is able to.
*/
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
if (anchor.get() != null) {
/* The drag event on the root really takes place. */
drag(me, root);
}
}
});
} /**
* Drag the primary stage.
*
* @param me
* - the mouse event
* @param root
* - the root node of stage
*/
private void drag(MouseEvent me, Node root) {
Window primaryStage = root.getScene().getWindow();
if (root.getCursor() == Cursor.H_RESIZE) {
setWidth(me, primaryStage);
} else if (root.getCursor() == Cursor.V_RESIZE) {
setHeight(me, primaryStage);
} else if (root.getCursor() == Cursor.SE_RESIZE) {
setWidth(me, primaryStage);
setHeight(me, primaryStage);
} else {
/* moving the stage */
moveStage(me, primaryStage);
}
} /**
* Set the stage width.
*
* @param me
* - the mouse event
* @param primaryStage
* - the primary stage
*/
private void setWidth(MouseEvent me, Window primaryStage) {
double stageMinWidth = 600;
primaryStage.setWidth(Math.max(stageMinWidth, widthStage.get() + (me.getScreenX() - (anchor.get().getX() + primaryStage.getX()))));
} /**
* Set the stage height.
*
* @param me
* - the mouse event
* @param primaryStage
* - the primary stage
*/
private void setHeight(MouseEvent me, Window primaryStage) {
double stageMinHeight = 400;
primaryStage.setHeight(Math.max(stageMinHeight, heightStage.get() + (me.getScreenY() - (anchor.get().getY() + primaryStage.getY()))));
} /**
* Move the stage.
*
* @param me
* - the mouse event
* @param primaryStage
* - the primary stage
*/
private void moveStage(MouseEvent me, Window primaryStage) {
if (maximized.get()) {
anchor.set(new Point2D(((me.getScreenX() - primaryStage.getX()) / primaryStage.getWidth()) * backupWindowBounds.get().getWidth(), me
.getScreenY() - screen.getVisualBounds().getMinY()));
primaryStage.setWidth(backupWindowBounds.get().getWidth());
primaryStage.setHeight(backupWindowBounds.get().getHeight());
maximized.set(false);
}
primaryStage.setX(me.getScreenX() - anchor.get().getX());
primaryStage.setY(me.getScreenY() - anchor.get().getY());
}
/**
* 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
*/
private void enableDragging(final AnchorPane root) {
/*
* This mouse event is for resizing window. Define some specific cursor patterns.
*/
root.setOnMouseMoved(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
if (me.getX() > root.getWidth() - 10 && me.getX() < root.getWidth() + 10 && me.getY() > root.getHeight() - 10
&& me.getY() < root.getHeight() + 10) {
root.setCursor(Cursor.SE_RESIZE);
} else if (me.getX() > root.getWidth() - 5 && me.getX() < root.getWidth() + 5) {
root.setCursor(Cursor.H_RESIZE);
} else if (me.getY() > root.getHeight() - 5 && me.getY() < root.getHeight() + 5) {
root.setCursor(Cursor.V_RESIZE);
} else {
root.setCursor(Cursor.DEFAULT);
}
}
}); /* when mouse button is pressed, save the initial position of screen. */
root.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
Window primaryStage = root.getScene().getWindow();
anchor.set(new Point2D(me.getScreenX() - primaryStage.getX(), me.getScreenY() - primaryStage.getY()));
widthStage.set(primaryStage.getWidth());
heightStage.set(primaryStage.getHeight());
}
}); /*
* when mouse button is released, clear the initial position of screen. This action is very important, because it can be a judge to avoid
* mouse interaction with other nodes as ChoiceBox.
*/
root.setOnMouseReleased(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
anchor.set(null);
}
}); /*
* when screen is dragged, translate it accordingly. Note that the transparent pixels part contained in the root will not react to the drag.
* But translucent color is able to.
*/
root.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent me) {
if (anchor.get() != null) {
/* The drag event on the root really takes place. */
drag(me, root);
}
}
});
} /**
* Drag the primary stage.
*
* @param me
* - the mouse event
* @param root
* - the root node of stage
*/
private void drag(MouseEvent me, Node root) {
Window primaryStage = root.getScene().getWindow();
if (root.getCursor() == Cursor.H_RESIZE) {
setWidth(me, primaryStage);
} else if (root.getCursor() == Cursor.V_RESIZE) {
setHeight(me, primaryStage);
} else if (root.getCursor() == Cursor.SE_RESIZE) {
setWidth(me, primaryStage);
setHeight(me, primaryStage);
} else {
/* moving the stage */
moveStage(me, primaryStage);
}
} /**
* Set the stage width.
*
* @param me
* - the mouse event
* @param primaryStage
* - the primary stage
*/
private void setWidth(MouseEvent me, Window primaryStage) {
double stageMinWidth = 600;
primaryStage.setWidth(Math.max(stageMinWidth, widthStage.get() + (me.getScreenX() - (anchor.get().getX() + primaryStage.getX()))));
} /**
* Set the stage height.
*
* @param me
* - the mouse event
* @param primaryStage
* - the primary stage
*/
private void setHeight(MouseEvent me, Window primaryStage) {
double stageMinHeight = 400;
primaryStage.setHeight(Math.max(stageMinHeight, heightStage.get() + (me.getScreenY() - (anchor.get().getY() + primaryStage.getY()))));
} /**
* Move the stage.
*
* @param me
* - the mouse event
* @param primaryStage
* - the primary stage
*/
private void moveStage(MouseEvent me, Window primaryStage) {
if (maximized.get()) {
anchor.set(new Point2D(((me.getScreenX() - primaryStage.getX()) / primaryStage.getWidth()) * backupWindowBounds.get().getWidth(), me
.getScreenY() - screen.getVisualBounds().getMinY()));
primaryStage.setWidth(backupWindowBounds.get().getWidth());
primaryStage.setHeight(backupWindowBounds.get().getHeight());
maximized.set(false);
}
primaryStage.setX(me.getScreenX() - anchor.get().getX());
primaryStage.setY(me.getScreenY() - anchor.get().getY());
}
.getWhen(), e.getModifiers(), componentPoint.x,
componentPoint.y, e.getClickCount(), e.isPopupTrigger()));
一个最直接办法是利用Robot仿真模拟动作。
fireEvent方法不行吗?行啊,但是你要fire哪个e呢,得new 一个event
fireEvent方法不行吗?行啊,但是你要fire哪个e呢,得new 一个event
那个还原窗口大小的按钮是什么名字?从哪里得到?
fireEvent方法不行吗?行啊,但是你要fire哪个e呢,得new 一个event
那个还原窗口大小的按钮是什么名字?从哪里得到?就是createMaximumButton()和createMinmumButton()
fireEvent方法不行吗?行啊,但是你要fire哪个e呢,得new 一个event
那个还原窗口大小的按钮是什么名字?从哪里得到?就是createMaximumButton()和createMinmumButton()Stage hide()后,事件就阻塞了。能不能设置Stage让它不这样?
import javafx.stage.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.event.*;public class CloseFXWindow extends Application {
public static void main(final String... args) {
Application.launch(CloseFXWindow.class, args);
} @Override public void start(final Stage stage) {
stage.setTitle("FX Close");
Button close = ButtonBuilder.create().text("Close").onAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
Event.fireEvent(stage, new WindowEvent(stage, WindowEvent.WINDOW_CLOSE_REQUEST ));
}
}).build();
Button hidden = ButtonBuilder.create().text("Hidden").onAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
stage.setIconified(true);
}
}).build();
Button full = ButtonBuilder.create().text("Full Screen").onAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
stage.setFullScreen(true);
}
}).build();
Button max = ButtonBuilder.create().text("Max").onAction(new EventHandler<ActionEvent>(){
@Override public void handle(ActionEvent e){
javafx.geometry.Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stage.setX(primaryScreenBounds.getMinX());
stage.setY(primaryScreenBounds.getMinY());
stage.setWidth(primaryScreenBounds.getWidth());
stage.setHeight(primaryScreenBounds.getHeight());
}
}).build();
VBox vbox = new VBox(8);
vbox.getChildren().addAll(close, hidden, max, full);
Group root = new Group(vbox);
Scene scene = SceneBuilder.create().root(root).width(400).height(300).build();
stage.setScene(scene);
stage.show();
}
}
Stage hide()后,事件就阻塞了。能不能设置Stage让它不这样?