|
@@ -0,0 +1,181 @@
|
|
|
|
|
+package space.anyi.chatServer;
|
|
|
|
|
+
|
|
|
|
|
+import javafx.application.Application;
|
|
|
|
|
+import javafx.application.Platform;
|
|
|
|
|
+import javafx.concurrent.Task;
|
|
|
|
|
+import javafx.event.EventType;
|
|
|
|
|
+import javafx.scene.Scene;
|
|
|
|
|
+import javafx.scene.control.Alert;
|
|
|
|
|
+import javafx.scene.control.ButtonType;
|
|
|
|
|
+import javafx.scene.control.Label;
|
|
|
|
|
+import javafx.scene.paint.Color;
|
|
|
|
|
+import javafx.stage.Stage;
|
|
|
|
|
+import javafx.stage.WindowEvent;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @ProjectName: chat-gwng
|
|
|
|
|
+ * @FileName: Test
|
|
|
|
|
+ * @Author: 杨逸
|
|
|
|
|
+ * @Data:2025/9/24 20:09
|
|
|
|
|
+ * @Description: Javafx入门案例
|
|
|
|
|
+ */
|
|
|
|
|
+public class TestJAVAFX extends Application {
|
|
|
|
|
+
|
|
|
|
|
+ private Stage stage;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void start(Stage stage) throws Exception {
|
|
|
|
|
+ this.stage = stage;
|
|
|
|
|
+ //设置标题
|
|
|
|
|
+ stage.setTitle("Test JAVAFX");
|
|
|
|
|
+ //设置窗口大小
|
|
|
|
|
+ stage.setWidth(400);
|
|
|
|
|
+ stage.setHeight(400);
|
|
|
|
|
+ //固定窗口大小
|
|
|
|
|
+ stage.setResizable(false);
|
|
|
|
|
+ stage.setFullScreen(false);
|
|
|
|
|
+
|
|
|
|
|
+ //创建一个场景
|
|
|
|
|
+ Label label = new Label("Hello World");
|
|
|
|
|
+ final int[] data = {1};
|
|
|
|
|
+ label.setOnMouseClicked(event ->{
|
|
|
|
|
+ System.out.println("点击事件回调");
|
|
|
|
|
+ //更新UI使用Platform.runLater
|
|
|
|
|
+ Platform.runLater(()->{
|
|
|
|
|
+ label.setText(label.getText()+ data[0]);
|
|
|
|
|
+ data[0]++;
|
|
|
|
|
+ });
|
|
|
|
|
+ //更新UI使用Tasks
|
|
|
|
|
+ });
|
|
|
|
|
+ //LabeledText labeledText = new LabeledText(new LabeledImpl(label));
|
|
|
|
|
+ Scene scene = new Scene(label,400,400);
|
|
|
|
|
+ //设置场景背景填充
|
|
|
|
|
+ scene.setFill(Color.BLACK);
|
|
|
|
|
+ //设置场景
|
|
|
|
|
+ stage.setScene(scene);
|
|
|
|
|
+ //显示
|
|
|
|
|
+ stage.show();
|
|
|
|
|
+ //设置关闭回调
|
|
|
|
|
+ stage.setOnCloseRequest(windowEvent -> {
|
|
|
|
|
+ System.out.println("关闭事件回调");
|
|
|
|
|
+ EventType<WindowEvent> eventType = windowEvent.getEventType();
|
|
|
|
|
+ String name = eventType.getName();
|
|
|
|
|
+ System.out.println(name);
|
|
|
|
|
+ //消费事件,防止事件继续传递
|
|
|
|
|
+ windowEvent.consume();
|
|
|
|
|
+ //信息提示
|
|
|
|
|
+ Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
|
|
|
|
|
+ alert.setTitle("提示");
|
|
|
|
|
+ alert.setHeaderText("确定要退出吗?");
|
|
|
|
|
+ //alert.setContentText("确定要退出吗?");
|
|
|
|
|
+ //获取用户的点击结果
|
|
|
|
|
+ Optional<ButtonType> buttonType = alert.showAndWait();
|
|
|
|
|
+ if (ButtonType.OK.equals(buttonType.get())) {
|
|
|
|
|
+ System.out.println("用户点击了确定");
|
|
|
|
|
+ //关闭窗口
|
|
|
|
|
+ //stage.close();
|
|
|
|
|
+ //退出JavaFX程序
|
|
|
|
|
+ Platform.exit();
|
|
|
|
|
+ }
|
|
|
|
|
+ //一些释放资源的操作...
|
|
|
|
|
+ });
|
|
|
|
|
+ //验证页面刷新机制,UI更新必须在JavaFX线程中执行
|
|
|
|
|
+ // 在其他线程更新UI必须使用Platform.runLater方法,但需要JavaFX线程空闲时,才会执行,可能不会在预期内执行
|
|
|
|
|
+ new Thread(()->{
|
|
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
|
|
+ int finalI = i;
|
|
|
|
|
+ Platform.runLater(()->{
|
|
|
|
|
+ //没有有生效,但也没报错
|
|
|
|
|
+ label.setText(label.getText()+ finalI);
|
|
|
|
|
+ });
|
|
|
|
|
+ try {
|
|
|
|
|
+ Thread.sleep(1000);
|
|
|
|
|
+ } catch (InterruptedException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }).start();
|
|
|
|
|
+
|
|
|
|
|
+ //Platform.runLater(()->{
|
|
|
|
|
+ // for (int i = 0; i < 10; i++) {
|
|
|
|
|
+ // label.setText(label.getText()+ i);
|
|
|
|
|
+ // try {
|
|
|
|
|
+ // Thread.sleep(1000);
|
|
|
|
|
+ // } catch (InterruptedException e) {
|
|
|
|
|
+ // e.printStackTrace();
|
|
|
|
|
+ // }
|
|
|
|
|
+ // }
|
|
|
|
|
+ //});
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ launch();
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+class MyTask extends Task<String> {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void scheduled() {
|
|
|
|
|
+ super.scheduled();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 任务执行时触发的方法回调,可以更新JavaFX的UI
|
|
|
|
|
+ * @description:
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/24 22:06:30
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void running() {
|
|
|
|
|
+ super.running();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description: 任务执行成功时,触发的方法回调
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/24 22:07:03
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void succeeded() {
|
|
|
|
|
+ super.succeeded();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description: 任务被取消时,触发的方法回调
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/24 22:07:23
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void cancelled() {
|
|
|
|
|
+ super.cancelled();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description: 任务执行失败时,触发的方法回调
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/24 22:07:35
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected void failed() {
|
|
|
|
|
+ super.failed();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 执行业务的方法,并返回结果
|
|
|
|
|
+ * @return {@code String }
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ * @description:
|
|
|
|
|
+ * @author: 杨逸
|
|
|
|
|
+ * @data:2025/09/24 22:05:57
|
|
|
|
|
+ * @since 1.0.0
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ protected String call() throws Exception {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|