完成窗口切换功能
This commit is contained in:
parent
b07515d544
commit
a3d288fab2
|
@ -1,6 +1,7 @@
|
|||
package org.rococy.roomit;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.paint.Paint;
|
||||
|
@ -20,14 +21,28 @@ import java.util.Objects;
|
|||
*/
|
||||
public class MainWindow extends Application {
|
||||
|
||||
private static Stage stage;
|
||||
|
||||
static {
|
||||
try {
|
||||
// 初始化托盘图标
|
||||
initTrayIcon();
|
||||
// 设置当程序所有窗口都关闭时,JavaFx的UI线程继续运行
|
||||
Platform.setImplicitExit(false);
|
||||
} catch (IOException | AWTException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start(Stage stage) throws IOException, AWTException {
|
||||
public void start(Stage stage) throws IOException {
|
||||
MainWindow.stage = stage;
|
||||
// 加载FXML文件
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(MainWindow.class.getResource("fxml/mainWindow.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load(), ScreenUtils.getScreenWidth(), ScreenUtils.getScreenHeight());
|
||||
|
||||
// 场景背景透明
|
||||
scene.setFill(Paint.valueOf("#00000000"));
|
||||
|
||||
// 设置场景
|
||||
stage.setScene(scene);
|
||||
// 设置全屏
|
||||
|
@ -40,16 +55,26 @@ public class MainWindow extends Application {
|
|||
stage.initStyle(StageStyle.TRANSPARENT);
|
||||
// 将窗口显示出来
|
||||
stage.show();
|
||||
|
||||
// 初始化托盘图标
|
||||
initTrayIcon();
|
||||
}
|
||||
|
||||
private void initTrayIcon() throws IOException, AWTException {
|
||||
public void show() {
|
||||
if (stage.isShowing()) {
|
||||
stage.close();
|
||||
System.gc();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
start(new Stage());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void initTrayIcon() throws IOException, AWTException {
|
||||
// 获取系统托盘
|
||||
SystemTray systemTray = SystemTray.getSystemTray();
|
||||
// 加载托盘图片 16*16的png图片
|
||||
URL imageUrl = Objects.requireNonNull(this.getClass().getResource("/org/rococy/roomit/img/a.png"));
|
||||
URL imageUrl = Objects.requireNonNull(MainWindow.class.getResource("/org/rococy/roomit/img/a.png"));
|
||||
TrayIcon trayIcon = new TrayIcon(ImageIO.read(imageUrl), "RoomIt");
|
||||
// 托盘图片自适应大小
|
||||
trayIcon.setImageAutoSize(true);
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package org.rococy.roomit;
|
||||
|
||||
import javafx.application.Application;
|
||||
import javafx.application.Platform;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.paint.Paint;
|
||||
|
@ -26,19 +25,26 @@ public class TrayWindow extends Application {
|
|||
// 加载FXML文件
|
||||
FXMLLoader fxmlLoader = new FXMLLoader(TrayWindow.class.getResource("fxml/trayWindow.fxml"));
|
||||
Scene scene = new Scene(fxmlLoader.load());
|
||||
|
||||
// 设置场景
|
||||
stage.setScene(scene);
|
||||
// 场景填充透明背景
|
||||
scene.setFill(Paint.valueOf("#00000000"));
|
||||
|
||||
// 取消任务栏
|
||||
stage.initStyle(StageStyle.TRANSPARENT);
|
||||
// 置顶
|
||||
stage.setAlwaysOnTop(true);
|
||||
// 设置当程序所有窗口都关闭时,JavaFx的UI线程继续运行
|
||||
Platform.setImplicitExit(false);
|
||||
|
||||
// 当没有焦点时关闭
|
||||
stage.focusedProperty().addListener((observable, oldValue, newValue) -> {
|
||||
if (newValue) {
|
||||
return;
|
||||
}
|
||||
stage.close();
|
||||
});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -46,18 +52,13 @@ public class TrayWindow extends Application {
|
|||
if (stage.isShowing()) {
|
||||
return;
|
||||
}
|
||||
// 将窗口显示出来
|
||||
stage.show();
|
||||
}
|
||||
|
||||
public void show(double x, double y) {
|
||||
// Screen.getPrimary().getOutputScaleX获取屏幕缩放比
|
||||
stage.setX(x / Screen.getPrimary().getOutputScaleX() - 215);
|
||||
stage.setY(y / Screen.getPrimary().getOutputScaleY() - 80);
|
||||
try {
|
||||
stage.setX(x / Screen.getPrimary().getOutputScaleX() - 210);
|
||||
stage.setY(y / Screen.getPrimary().getOutputScaleY() - 75);
|
||||
start(stage);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ import javafx.scene.input.MouseEvent;
|
|||
import javafx.scene.layout.Background;
|
||||
import javafx.scene.shape.Shape;
|
||||
import javafx.stage.Stage;
|
||||
import javafx.stage.WindowEvent;
|
||||
import org.rococy.roomit.control.Container;
|
||||
import org.rococy.roomit.control.*;
|
||||
import org.rococy.roomit.cursor.RoomItCursor;
|
||||
|
@ -164,6 +165,7 @@ public class MainWindowController implements Initializable {
|
|||
container.addEventHandler(MouseEvent.MOUSE_DRAGGED, paneMouseEventHandler);
|
||||
container.addEventHandler(MouseEvent.MOUSE_ENTERED, paneMouseEventHandler);
|
||||
container.addEventHandler(MouseEvent.MOUSE_MOVED, paneMouseEventHandler);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -208,6 +210,7 @@ public class MainWindowController implements Initializable {
|
|||
cursor.setOpacity(1);
|
||||
mouseLogo.setOpacity(1);
|
||||
});
|
||||
|
||||
// 将默认鼠标隐藏,使用自定义鼠标
|
||||
container.setCursor(Cursor.NONE);
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package org.rococy.roomit.listener;
|
||||
|
||||
import javafx.application.Platform;
|
||||
import org.rococy.roomit.MainWindow;
|
||||
import org.rococy.roomit.TrayWindow;
|
||||
|
||||
import java.awt.event.MouseEvent;
|
||||
|
@ -24,9 +25,7 @@ public class TrayIconMouseListener implements MouseListener {
|
|||
public void mousePressed(MouseEvent e) {
|
||||
switch (e.getButton()) {
|
||||
// 左键
|
||||
case MOUSE_LEFT_BUTTON -> {
|
||||
|
||||
}
|
||||
case MOUSE_LEFT_BUTTON -> Platform.runLater(() -> new MainWindow().show());
|
||||
|
||||
// 右键
|
||||
case MOUSE_RIGHT_BUTTON -> {
|
||||
|
@ -34,7 +33,6 @@ public class TrayIconMouseListener implements MouseListener {
|
|||
if (trayWindow == null) {
|
||||
trayWindow = new TrayWindow();
|
||||
}
|
||||
|
||||
trayWindow.show(e.getXOnScreen(), e.getYOnScreen());
|
||||
});
|
||||
}
|
||||
|
@ -44,16 +42,13 @@ public class TrayIconMouseListener implements MouseListener {
|
|||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"shape": {
|
||||
"brush": {
|
||||
"transparent": true,
|
||||
"transparent": false,
|
||||
"strokeColor": "#ff0000ff",
|
||||
"lineWidth": 4.0
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue