From 77bf1f0eb3e35bea2cc7b359f3eba16f83a2e90a Mon Sep 17 00:00:00 2001 From: yang <1633346816@qq.com> Date: Mon, 17 Oct 2022 15:09:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/rococy/roomit/MainWindow.java | 39 +++++++++---------- .../rococy/roomit/control/base/Resizer.java | 36 +++++------------ 2 files changed, 29 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/rococy/roomit/MainWindow.java b/src/main/java/org/rococy/roomit/MainWindow.java index 1fc1b5c..636de3e 100644 --- a/src/main/java/org/rococy/roomit/MainWindow.java +++ b/src/main/java/org/rococy/roomit/MainWindow.java @@ -1,6 +1,5 @@ package org.rococy.roomit; -import javafx.application.Application; import javafx.application.Platform; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; @@ -22,9 +21,9 @@ import java.io.IOException; /** * @author Rococy */ -public class MainWindow extends Application { +public class MainWindow { - private static Stage stage = new Stage(); + private static Stage stage; static { // 设置全局快捷键 @@ -39,14 +38,25 @@ public class MainWindow extends Application { }); } - @Override - public void start(Stage stage) { - launch(stage); + /** + * 显示或关闭窗口 + */ + public static void toggleDisplay() { + if (stage != null && stage.isShowing()) { + stage.close(); + System.gc(); + } else { + launch(); + } } - public static void launch(Stage stage) { + /** + * 启动窗口 + */ + private static void launch() { try { - MainWindow.stage = stage; + // 创建新窗口 + stage = new Stage(); // 加载FXML文件 FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtils.getResource("fxml/mainWindow.fxml")); Scene scene = new Scene(fxmlLoader.load(), ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight()); @@ -69,20 +79,9 @@ public class MainWindow extends Application { stage.getIcons().add(new Image(ResourceUtils.getResourceAsStream(GlobalConsts.LOGO_PATH))); // 将窗口显示出来 stage.show(); - } catch ( - IOException e) { + } catch (IOException e) { e.printStackTrace(); } } - - public static void toggleDisplay() { - if (stage.isShowing()) { - stage.close(); - System.gc(); - } else { - launch(new Stage()); - } - } - } \ No newline at end of file diff --git a/src/main/java/org/rococy/roomit/control/base/Resizer.java b/src/main/java/org/rococy/roomit/control/base/Resizer.java index 4fb7566..cad9376 100644 --- a/src/main/java/org/rococy/roomit/control/base/Resizer.java +++ b/src/main/java/org/rococy/roomit/control/base/Resizer.java @@ -229,56 +229,41 @@ public abstract class Resizer extends Pane { switch (rectangleCtrl) { - case TOP_LEFT: { + case TOP_LEFT -> { this.setLayoutX(screenX); this.setLayoutY(screenY); this.setPrefSize(width - x, height - y); - break; } - case TOP_MIDDLE: { + case TOP_MIDDLE -> { this.setLayoutY(screenY); this.setPrefHeight(height - y); - break; } - case TOP_RIGHT: { + case TOP_RIGHT -> { this.setLayoutY(screenY); this.setPrefSize(x, height - y); - break; } - case CENTER_LEFT: { + case CENTER_LEFT -> { this.setLayoutX(screenX); this.setPrefWidth(width - x); - break; } - case CENTER_RIGHT: { - this.setPrefWidth(x); - break; - } + case CENTER_RIGHT -> this.setPrefWidth(x); - case BOTTOM_LEFT: { + case BOTTOM_LEFT -> { this.setLayoutX(screenX); this.setPrefSize(width - x, y); - break; } - case BOTTOM_MIDDLE: { - this.setPrefHeight(y); - break; - } + case BOTTOM_MIDDLE -> this.setPrefHeight(y); - case BOTTOM_RIGHT: { - this.setPrefSize(x, y); - break; - } + case BOTTOM_RIGHT -> this.setPrefSize(x, y); - case MOVE: { + case MOVE -> { this.setLayoutX(screenX - moveX); this.setLayoutY(screenY - moveY); - break; } } }); @@ -301,11 +286,10 @@ public abstract class Resizer extends Pane { private void bindKeyPressedEvent() { this.addEventHandler(KeyEvent.KEY_PRESSED, e -> { switch (e.getCode()) { - case DELETE: { + case DELETE -> { // 删除自身 Pane parent = (Pane) this.getParent(); parent.getChildren().remove(this); - break; } } });