From 05d9a6ca042c6bf97e947e0aa43c207fd1bfbb0d Mon Sep 17 00:00:00 2001 From: RedDragon0293 <1352003034@qq.com> Date: Mon, 25 Sep 2023 21:27:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=93=BE=E6=8E=A5=E6=A0=A1?= =?UTF-8?q?=E5=9B=AD=E7=BD=91=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=88=96=E6=97=A0=E7=BD=91=E7=BB=9C=E8=BF=9E=E6=8E=A5=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/reddragon/eportal/Authenticator.java | 37 ++++++++++++++++++- .../java/cn/reddragon/eportal/Config.java | 6 ++- .../reddragon/eportal/HelloApplication.java | 13 +++---- .../cn/reddragon/eportal/HelloController.java | 5 ++- 4 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/main/java/cn/reddragon/eportal/Authenticator.java b/src/main/java/cn/reddragon/eportal/Authenticator.java index 2f5745a..41e9582 100644 --- a/src/main/java/cn/reddragon/eportal/Authenticator.java +++ b/src/main/java/cn/reddragon/eportal/Authenticator.java @@ -2,9 +2,12 @@ import cn.reddragon.eportal.utils.HttpUtils; +import java.awt.*; import java.io.IOException; import java.io.PrintWriter; import java.net.HttpURLConnection; +import java.net.NoRouteToHostException; +import java.net.SocketTimeoutException; import java.util.List; import java.util.Map; import java.util.Objects; @@ -77,7 +80,39 @@ public static boolean isOnline() { online = false; return false; } - } catch (Exception e) { + } catch (SocketTimeoutException e) { + if (SystemTray.isSupported()) { + SystemTray tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage("icon.png"); + TrayIcon icon = new TrayIcon(image, "Error"); + icon.setImageAutoSize(true); + try { + tray.add(icon); + } catch (AWTException e1) { + throw new RuntimeException(e1); + } + icon.displayMessage("Error", "Connection timeout!", TrayIcon.MessageType.ERROR); + tray.remove(icon); + System.exit(0); + } + return false; + } catch (NoRouteToHostException e) { + if (SystemTray.isSupported()) { + SystemTray tray = SystemTray.getSystemTray(); + Image image = Toolkit.getDefaultToolkit().createImage("icon.png"); + TrayIcon icon = new TrayIcon(image, "Error"); + icon.setImageAutoSize(true); + try { + tray.add(icon); + } catch (AWTException e1) { + throw new RuntimeException(e1); + } + icon.displayMessage("Error", "No Internet connection!", TrayIcon.MessageType.ERROR); + tray.remove(icon); + System.exit(0); + } + return false; + } catch (IOException e) { online = false; return false; } diff --git a/src/main/java/cn/reddragon/eportal/Config.java b/src/main/java/cn/reddragon/eportal/Config.java index ea02089..3ad4f78 100644 --- a/src/main/java/cn/reddragon/eportal/Config.java +++ b/src/main/java/cn/reddragon/eportal/Config.java @@ -1,6 +1,9 @@ package cn.reddragon.eportal; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -47,7 +50,6 @@ public static String[] read() throws IOException { } String password = new String(b1); stream.close(); - System.out.println(username + " " + password); return new String[]{username, password}; } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/cn/reddragon/eportal/HelloApplication.java b/src/main/java/cn/reddragon/eportal/HelloApplication.java index 7f1643a..aea1bc3 100644 --- a/src/main/java/cn/reddragon/eportal/HelloApplication.java +++ b/src/main/java/cn/reddragon/eportal/HelloApplication.java @@ -12,14 +12,19 @@ import javafx.scene.control.TextField; import javafx.stage.Stage; -import java.io.IOException; import java.awt.*; +import java.io.IOException; public class HelloApplication extends Application { private static Label statusLabel; private static Button button; public static final Thread askThread = new Thread(() -> { while (true) { + try { + Thread.sleep(2500L); + } catch (InterruptedException e) { + //throw new RuntimeException(e); + } Authenticator.isOnline(); Platform.runLater(() -> { statusLabel.setText("Current status:" + (Authenticator.online ? "Online" : "Offline")); @@ -29,11 +34,6 @@ public class HelloApplication extends Application { button.setText("Login"); } }); - try { - Thread.sleep(2500L); - } catch (InterruptedException e) { - //throw new RuntimeException(e); - } } }); @@ -84,7 +84,6 @@ public static void main(String[] args) { e.printStackTrace(); System.exit(0); } catch (IOException ignored) { - } launch(); System.exit(0); diff --git a/src/main/java/cn/reddragon/eportal/HelloController.java b/src/main/java/cn/reddragon/eportal/HelloController.java index ed51f3c..6e49584 100644 --- a/src/main/java/cn/reddragon/eportal/HelloController.java +++ b/src/main/java/cn/reddragon/eportal/HelloController.java @@ -3,7 +3,8 @@ import cn.reddragon.eportal.utils.HttpUtils; import cn.reddragon.eportal.utils.IOUtils; import cn.reddragon.eportal.utils.URIEncoder; -import com.google.gson.*; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.ChoiceBox; @@ -29,7 +30,7 @@ public class HelloController { @FXML protected void onLoginButtonClick() { - if (Authenticator.isOnline()) { + if (Authenticator.online) { if (button.getText().equals("Login")) { resultText.setText("Already logged in!"); button.setText("Logout");