commit 279e6c33e8a948594034317a73fccb6d2a2f01c2 Author: skxjx2024. Date: Mon Jun 23 22:56:15 2025 +0800 first commit diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/MarsCodeWorkspaceAppSettings.xml b/.idea/MarsCodeWorkspaceAppSettings.xml new file mode 100644 index 0000000..05ed8ba --- /dev/null +++ b/.idea/MarsCodeWorkspaceAppSettings.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/libraries/json_20250517.xml b/.idea/libraries/json_20250517.xml new file mode 100644 index 0000000..4e341ca --- /dev/null +++ b/.idea/libraries/json_20250517.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..69cf16d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2524037 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c995aa5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/TestFX.class b/TestFX.class new file mode 100644 index 0000000..0b5b834 Binary files /dev/null and b/TestFX.class differ diff --git a/TestFX.java b/TestFX.java new file mode 100644 index 0000000..c8c8c56 --- /dev/null +++ b/TestFX.java @@ -0,0 +1,13 @@ +import javafx.application.Application; +import javafx.stage.Stage; + +public class TestFX extends Application { + @Override + public void start(Stage stage) { + stage.setTitle("Test"); + stage.show(); + } + public static void main(String[] args) { + launch(args); + } +} \ No newline at end of file diff --git a/bin/AutoStartManager.class b/bin/AutoStartManager.class new file mode 100644 index 0000000..b7c4f60 Binary files /dev/null and b/bin/AutoStartManager.class differ diff --git a/bin/SceneManager.class b/bin/SceneManager.class new file mode 100644 index 0000000..e6e1f46 Binary files /dev/null and b/bin/SceneManager.class differ diff --git a/json-20250517.jar b/json-20250517.jar new file mode 100644 index 0000000..0dc6cfc Binary files /dev/null and b/json-20250517.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..db78cb2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,44 @@ + + + org.openjfx + javafx-controls + 17.0.2 + + + org.openjfx + javafx-fxml + 17.0.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 17 + 17 + + + + org.openjfx + javafx-maven-plugin + 0.0.8 + + + default-cli + + run + + + + + + + + 17 + 17 + UTF-8 + diff --git a/src/AutoStartManager.java b/src/AutoStartManager.java new file mode 100644 index 0000000..783324f --- /dev/null +++ b/src/AutoStartManager.java @@ -0,0 +1,38 @@ +import java.io.File; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.util.prefs.Preferences; + +public class AutoStartManager { + public static void setAutoStart(boolean enable) { + Preferences prefs = Preferences.userNodeForPackage(AutoStartManager.class); + prefs.putBoolean("auto_start", enable); + + if (enable) { + try { + // 获取当前JAR路径(开发阶段使用IDE运行路径) + String jarPath = new File( + WeatherApp.class.getProtectionDomain() + .getCodeSource().getLocation().toURI() + ).getPath(); + + // Windows启动文件夹路径 + String startupDir = System.getenv("APPDATA") + + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup"; + File shortcut = new File(startupDir, "WeatherApp.lnk"); + + // 复制快捷方式(实际需使用JNA创建,此处简化为文件复制) + Files.copy(new File(jarPath).toPath(), + shortcut.toPath(), + StandardCopyOption.REPLACE_EXISTING); + } catch (Exception e) { + e.printStackTrace(); + } + } else { + // 删除启动项 + String startupPath = System.getenv("APPDATA") + + "\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\WeatherApp.lnk"; + new File(startupPath).delete(); + } + } +} \ No newline at end of file diff --git a/src/SceneManager.java b/src/SceneManager.java new file mode 100644 index 0000000..b127a1c --- /dev/null +++ b/src/SceneManager.java @@ -0,0 +1,25 @@ +import javafx.scene.Scene; +import javafx.scene.effect.GaussianBlur; +import javafx.scene.layout.StackPane; +import javafx.scene.paint.Color; +import javafx.scene.text.Text; + +public class SceneManager { + private static Text weatherText = new Text("加载天气中..."); + + public static Scene createMainScene() { + StackPane root = new StackPane(); + root.setStyle("-fx-background-color: rgba(255, 255, 255, 0.3);"); + root.setEffect(new GaussianBlur(10)); + root.getChildren().add(weatherText); + + Scene scene = new Scene(root); + scene.setFill(Color.TRANSPARENT); + return scene; + } + + public static void updateWeatherDisplay(String weatherData) { + // 解析JSON并更新显示(示例) + weatherText.setText("当前天气:晴\n温度:25℃"); + } +} \ No newline at end of file diff --git a/src/SettingsDialog.java b/src/SettingsDialog.java new file mode 100644 index 0000000..edac63a --- /dev/null +++ b/src/SettingsDialog.java @@ -0,0 +1,36 @@ +package src; + +import javafx.scene.control.*; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; +import java.util.prefs.Preferences; + +public class SettingsDialog extends Dialog { + public SettingsDialog(Stage owner) { + initOwner(owner); + setTitle("设置"); + + Preferences prefs = Preferences.userNodeForPackage(SettingsDialog.class); + TextField apiKeyField = new TextField(prefs.get("baidu_api_key", "")); + CheckBox autoStartCheck = new CheckBox("开机自启动"); + autoStartCheck.setSelected(prefs.getBoolean("auto_start", false)); + + VBox content = new VBox(15); + content.getChildren().addAll( + new Label("百度地图API密钥:"), apiKeyField, + autoStartCheck + ); + + getDialogPane().setContent(content); + + ButtonType saveButton = new ButtonType("保存", ButtonBar.ButtonData.OK_DONE); + getDialogPane().getButtonTypes().addAll(saveButton, ButtonType.CANCEL); + + // 修正:仅保留一次事件绑定 + getDialogPane().lookupButton(saveButton).setOnAction(e -> { + prefs.put("baidu_api_key", apiKeyField.getText()); + prefs.putBoolean("auto_start", autoStartCheck.isSelected()); + AutoStartManager.setAutoStart(autoStartCheck.isSelected()); + }); + } +} \ No newline at end of file diff --git a/src/SystemTrayIntegration.java b/src/SystemTrayIntegration.java new file mode 100644 index 0000000..486ec64 --- /dev/null +++ b/src/SystemTrayIntegration.java @@ -0,0 +1,40 @@ +import java.awt.*; +import java.awt.image.BufferedImage; +import javafx.stage.Stage; + +public class SystemTrayIntegration { + public static void createTrayIcon(Stage primaryStage) { + if (!SystemTray.isSupported()) return; + + SystemTray tray = SystemTray.getSystemTray(); + PopupMenu popup = new PopupMenu(); + + // 显示主窗口菜单项 + MenuItem showItem = new MenuItem("显示窗口"); + showItem.addActionListener(e -> primaryStage.show()); + + // 设置菜单项 + MenuItem settingsItem = new MenuItem("设置"); + settingsItem.addActionListener(e -> new SettingsDialog(primaryStage).showAndWait()); + + // 退出菜单项 + MenuItem exitItem = new MenuItem("退出"); + exitItem.addActionListener(e -> primaryStage.close()); + + popup.add(showItem); + popup.add(settingsItem); + popup.addSeparator(); + popup.add(exitItem); + + // 创建托盘图标(使用16x16透明图标) + BufferedImage icon = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB); + TrayIcon trayIcon = new TrayIcon(icon, "桌面天气", popup); + trayIcon.setImageAutoSize(true); + + try { + tray.add(trayIcon); + } catch (AWTException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/src/WeatherApp.java b/src/WeatherApp.java new file mode 100644 index 0000000..15766c9 --- /dev/null +++ b/src/WeatherApp.java @@ -0,0 +1,44 @@ +package src; + +import javafx.application.Application; +import javafx.application.Platform; +import javafx.stage.Stage; +import java.awt.*; +import java.util.prefs.Preferences; + +public class WeatherApp extends Application { + private WeatherService weatherService; + + public static void main(String[] args) { + // 检查开机自启动配置 + Preferences prefs = Preferences.userNodeForPackage(WeatherApp.class); + if (prefs.getBoolean("auto_start", false)) { + launch(args); + } + } + + @Override + public void start(Stage primaryStage) { + // 初始化天气服务 + weatherService = new WeatherService(); + weatherService.startWeatherUpdates(); + + // 创建系统托盘 + SystemTrayIntegration.createTrayIcon(primaryStage); + + // 配置主窗口(毛玻璃效果) + primaryStage.setTitle("桌面天气"); + primaryStage.setScene(SceneManager.createMainScene()); + primaryStage.setWidth(300); + primaryStage.setHeight(400); + primaryStage.setResizable(false); + primaryStage.show(); + } + + @Override + public void stop() { + weatherService.stopWeatherUpdates(); + Platform.exit(); + System.exit(0); + } +} \ No newline at end of file diff --git a/src/WeatherService.java b/src/WeatherService.java new file mode 100644 index 0000000..c84bd53 --- /dev/null +++ b/src/WeatherService.java @@ -0,0 +1,58 @@ +import java.util.Timer; +import java.util.TimerTask; +import java.util.prefs.Preferences; +import javafx.application.Platform; +// 修正:将import语句移至类定义前 +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +public class WeatherService { + private Timer updateTimer; + private final String BAIDU_WEATHER_API = "https://api.map.baidu.com/weather/v1/"; + + public void startWeatherUpdates() { + updateTimer = new Timer(); + updateTimer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + fetchWeatherData(); + } + }, 0, 60 * 1000); // 每分钟更新 + } + + private void fetchWeatherData() { + // 获取保存的API密钥 + Preferences prefs = Preferences.userNodeForPackage(WeatherService.class); + String apiKey = prefs.get("baidu_api_key", ""); + + if (apiKey.isEmpty()) return; + + // 调用百度天气API(伪代码) + try { + // 修正:使用JDK原生HttpClient替换HttpUtil + String result = fetchWeatherData(apiKey); + Platform.runLater(() -> { + SceneManager.updateWeatherDisplay(result); + }); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void stopWeatherUpdates() { + if (updateTimer != null) { + updateTimer.cancel(); + } + } + + private String fetchWeatherData(String apiKey) throws Exception { + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(BAIDU_WEATHER_API + "?ak=" + apiKey)) + .build(); + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + return response.body(); + } +} \ No newline at end of file diff --git a/src/pom.xml b/src/pom.xml new file mode 100644 index 0000000..1dd7123 --- /dev/null +++ b/src/pom.xml @@ -0,0 +1,39 @@ + + + org.openjfx + javafx-controls + 17.0.2 + + + org.openjfx + javafx-fxml + 17.0.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 17 + 17 + + + + org.openjfx + javafx-maven-plugin + 0.0.8 + + + default-cli + + run + + + + + + diff --git a/target/classes/.idea/.gitignore b/target/classes/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/target/classes/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/target/classes/.idea/MarsCodeWorkspaceAppSettings.xml b/target/classes/.idea/MarsCodeWorkspaceAppSettings.xml new file mode 100644 index 0000000..05ed8ba --- /dev/null +++ b/target/classes/.idea/MarsCodeWorkspaceAppSettings.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/target/classes/.idea/libraries/json_20250517.xml b/target/classes/.idea/libraries/json_20250517.xml new file mode 100644 index 0000000..4e341ca --- /dev/null +++ b/target/classes/.idea/libraries/json_20250517.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/target/classes/.idea/misc.xml b/target/classes/.idea/misc.xml new file mode 100644 index 0000000..69cf16d --- /dev/null +++ b/target/classes/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/target/classes/.idea/modules.xml b/target/classes/.idea/modules.xml new file mode 100644 index 0000000..2524037 --- /dev/null +++ b/target/classes/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/target/classes/.idea/vcs.xml b/target/classes/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/target/classes/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/target/classes/.vscode/settings.json b/target/classes/.vscode/settings.json new file mode 100644 index 0000000..c995aa5 --- /dev/null +++ b/target/classes/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/target/classes/json-20250517.jar b/target/classes/json-20250517.jar new file mode 100644 index 0000000..0dc6cfc Binary files /dev/null and b/target/classes/json-20250517.jar differ diff --git a/target/classes/pom.xml b/target/classes/pom.xml new file mode 100644 index 0000000..db78cb2 --- /dev/null +++ b/target/classes/pom.xml @@ -0,0 +1,44 @@ + + + org.openjfx + javafx-controls + 17.0.2 + + + org.openjfx + javafx-fxml + 17.0.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 17 + 17 + + + + org.openjfx + javafx-maven-plugin + 0.0.8 + + + default-cli + + run + + + + + + + + 17 + 17 + UTF-8 + diff --git a/target/classes/src/pom.xml b/target/classes/src/pom.xml new file mode 100644 index 0000000..1dd7123 --- /dev/null +++ b/target/classes/src/pom.xml @@ -0,0 +1,39 @@ + + + org.openjfx + javafx-controls + 17.0.2 + + + org.openjfx + javafx-fxml + 17.0.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + 17 + 17 + + + + org.openjfx + javafx-maven-plugin + 0.0.8 + + + default-cli + + run + + + + + + diff --git a/target/classes/weather.iml b/target/classes/weather.iml new file mode 100644 index 0000000..ee9a001 --- /dev/null +++ b/target/classes/weather.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/weather.iml b/weather.iml new file mode 100644 index 0000000..ee9a001 --- /dev/null +++ b/weather.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file