优化代码

This commit is contained in:
yang 2022-10-17 15:09:13 +08:00
parent 2f24113d98
commit 77bf1f0eb3
2 changed files with 29 additions and 46 deletions

View File

@ -1,6 +1,5 @@
package org.rococy.roomit; package org.rococy.roomit;
import javafx.application.Application;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.fxml.FXMLLoader; import javafx.fxml.FXMLLoader;
import javafx.scene.Scene; import javafx.scene.Scene;
@ -22,9 +21,9 @@ import java.io.IOException;
/** /**
* @author Rococy * @author Rococy
*/ */
public class MainWindow extends Application { public class MainWindow {
private static Stage stage = new Stage(); private static Stage stage;
static { 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 { try {
MainWindow.stage = stage; // 创建新窗口
stage = new Stage();
// 加载FXML文件 // 加载FXML文件
FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtils.getResource("fxml/mainWindow.fxml")); FXMLLoader fxmlLoader = new FXMLLoader(ResourceUtils.getResource("fxml/mainWindow.fxml"));
Scene scene = new Scene(fxmlLoader.load(), ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight()); 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.getIcons().add(new Image(ResourceUtils.getResourceAsStream(GlobalConsts.LOGO_PATH)));
// 将窗口显示出来 // 将窗口显示出来
stage.show(); stage.show();
} catch ( } catch (IOException e) {
IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void toggleDisplay() {
if (stage.isShowing()) {
stage.close();
System.gc();
} else {
launch(new Stage());
}
}
} }

View File

@ -229,56 +229,41 @@ public abstract class Resizer extends Pane {
switch (rectangleCtrl) { switch (rectangleCtrl) {
case TOP_LEFT: { case TOP_LEFT -> {
this.setLayoutX(screenX); this.setLayoutX(screenX);
this.setLayoutY(screenY); this.setLayoutY(screenY);
this.setPrefSize(width - x, height - y); this.setPrefSize(width - x, height - y);
break;
} }
case TOP_MIDDLE: { case TOP_MIDDLE -> {
this.setLayoutY(screenY); this.setLayoutY(screenY);
this.setPrefHeight(height - y); this.setPrefHeight(height - y);
break;
} }
case TOP_RIGHT: { case TOP_RIGHT -> {
this.setLayoutY(screenY); this.setLayoutY(screenY);
this.setPrefSize(x, height - y); this.setPrefSize(x, height - y);
break;
} }
case CENTER_LEFT: { case CENTER_LEFT -> {
this.setLayoutX(screenX); this.setLayoutX(screenX);
this.setPrefWidth(width - x); this.setPrefWidth(width - x);
break;
} }
case CENTER_RIGHT: { case CENTER_RIGHT -> this.setPrefWidth(x);
this.setPrefWidth(x);
break;
}
case BOTTOM_LEFT: { case BOTTOM_LEFT -> {
this.setLayoutX(screenX); this.setLayoutX(screenX);
this.setPrefSize(width - x, y); this.setPrefSize(width - x, y);
break;
} }
case BOTTOM_MIDDLE: { case BOTTOM_MIDDLE -> this.setPrefHeight(y);
this.setPrefHeight(y);
break;
}
case BOTTOM_RIGHT: { case BOTTOM_RIGHT -> this.setPrefSize(x, y);
this.setPrefSize(x, y);
break;
}
case MOVE: { case MOVE -> {
this.setLayoutX(screenX - moveX); this.setLayoutX(screenX - moveX);
this.setLayoutY(screenY - moveY); this.setLayoutY(screenY - moveY);
break;
} }
} }
}); });
@ -301,11 +286,10 @@ public abstract class Resizer extends Pane {
private void bindKeyPressedEvent() { private void bindKeyPressedEvent() {
this.addEventHandler(KeyEvent.KEY_PRESSED, e -> { this.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
switch (e.getCode()) { switch (e.getCode()) {
case DELETE: { case DELETE -> {
// 删除自身 // 删除自身
Pane parent = (Pane) this.getParent(); Pane parent = (Pane) this.getParent();
parent.getChildren().remove(this); parent.getChildren().remove(this);
break;
} }
} }
}); });