first commit

This commit is contained in:
skxjx2024. 2025-06-23 22:56:15 +08:00
commit 279e6c33e8
32 changed files with 544 additions and 0 deletions

8
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

6
.idea/MarsCodeWorkspaceAppSettings.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="com.codeverse.userSettings.MarscodeWorkspaceAppSettingsState">
<option name="ckgOperationStatus" value="SUCCESS" />
</component>
</project>

9
.idea/libraries/json_20250517.xml generated Normal file
View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="json-20250517">
<CLASSES>
<root url="jar://$PROJECT_DIR$/json-20250517.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

14
.idea/misc.xml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/weather.iml" filepath="$PROJECT_DIR$/weather.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"java.debug.settings.onBuildFailureProceed": true
}

BIN
TestFX.class Normal file

Binary file not shown.

13
TestFX.java Normal file
View File

@ -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);
}
}

BIN
bin/AutoStartManager.class Normal file

Binary file not shown.

BIN
bin/SceneManager.class Normal file

Binary file not shown.

BIN
json-20250517.jar Normal file

Binary file not shown.

44
pom.xml Normal file
View File

@ -0,0 +1,44 @@
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

38
src/AutoStartManager.java Normal file
View File

@ -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();
}
}
}

25
src/SceneManager.java Normal file
View File

@ -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℃");
}
}

36
src/SettingsDialog.java Normal file
View File

@ -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<Void> {
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());
});
}
}

View File

@ -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();
}
}
}

44
src/WeatherApp.java Normal file
View File

@ -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);
}
}

58
src/WeatherService.java Normal file
View File

@ -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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
}
}

39
src/pom.xml Normal file
View File

@ -0,0 +1,39 @@
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

8
target/classes/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="com.codeverse.userSettings.MarscodeWorkspaceAppSettingsState">
<option name="ckgOperationStatus" value="SUCCESS" />
</component>
</project>

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="json-20250517">
<CLASSES>
<root url="jar://$PROJECT_DIR$/json-20250517.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

14
target/classes/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_22" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
target/classes/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/weather.iml" filepath="$PROJECT_DIR$/weather.iml" />
</modules>
</component>
</project>

6
target/classes/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

3
target/classes/.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"java.debug.settings.onBuildFailureProceed": true
}

Binary file not shown.

44
target/classes/pom.xml Normal file
View File

@ -0,0 +1,44 @@
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

View File

@ -0,0 +1,39 @@
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.2</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>17.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<executions>
<execution>
<id>default-cli</id>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
</component>
</module>

8
weather.iml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
</component>
</module>