1+ package org .mcdcl .ui ;
2+
3+ import javafx .geometry .Insets ;
4+ import javafx .geometry .Pos ;
5+ import javafx .scene .control .Button ;
6+ import javafx .scene .control .Label ;
7+ import javafx .scene .control .ScrollPane ;
8+ import javafx .scene .control .TextArea ;
9+ import javafx .scene .layout .BorderPane ;
10+ import javafx .scene .layout .HBox ;
11+ import javafx .scene .layout .Priority ;
12+ import javafx .scene .layout .VBox ;
13+ import javafx .scene .paint .Color ;
14+ import javafx .scene .text .Font ;
15+ import javafx .scene .text .FontWeight ;
16+ import javafx .scene .text .Text ;
17+ import javafx .event .ActionEvent ;
18+ import javafx .event .EventHandler ;
19+
20+ /**
21+ * 错误信息显示页面
22+ * 用于展示游戏启动或运行过程中的错误信息
23+ */
24+ public class ErrorView extends BorderPane {
25+
26+ private String errorTitle ;
27+ private String errorMessage ;
28+ private String stackTrace ;
29+ private EventHandler <ActionEvent > returnHandler ;
30+
31+ public ErrorView (String errorTitle , String errorMessage , Throwable exception ) {
32+ this .errorTitle = errorTitle ;
33+ this .errorMessage = errorMessage ;
34+ this .stackTrace = getStackTraceAsString (exception );
35+
36+ initUI ();
37+ }
38+
39+ /**
40+ * 设置返回按钮的事件处理器
41+ * @param handler 返回按钮点击时的处理逻辑
42+ */
43+ public void setReturnHandler (EventHandler <ActionEvent > handler ) {
44+ this .returnHandler = handler ;
45+ }
46+
47+ private void initUI () {
48+ setPadding (new Insets (20 ));
49+
50+ // 顶部标题
51+ Text title = new Text ("启动错误" );
52+ title .setFont (Font .font ("System" , FontWeight .BOLD , 24 ));
53+ title .setFill (Color .RED );
54+
55+ // 错误标题
56+ Label errorTitleLabel = new Label (errorTitle );
57+ errorTitleLabel .setFont (Font .font ("System" , FontWeight .BOLD , 16 ));
58+ errorTitleLabel .setTextFill (Color .RED );
59+
60+ // 错误信息
61+ Label errorMessageLabel = new Label (errorMessage );
62+ errorMessageLabel .setWrapText (true );
63+
64+ // 堆栈跟踪
65+ TextArea stackTraceArea = new TextArea (stackTrace );
66+ stackTraceArea .setEditable (false );
67+ stackTraceArea .setWrapText (true );
68+ stackTraceArea .setPrefHeight (300 );
69+ VBox .setVgrow (stackTraceArea , Priority .ALWAYS );
70+
71+ // 创建滚动面板
72+ ScrollPane scrollPane = new ScrollPane (stackTraceArea );
73+ scrollPane .setFitToWidth (true );
74+ scrollPane .setFitToHeight (true );
75+
76+ // 底部按钮
77+ Button copyButton = new Button ("复制错误信息" );
78+ copyButton .setOnAction (e -> {
79+ javafx .scene .input .Clipboard clipboard = javafx .scene .input .Clipboard .getSystemClipboard ();
80+ javafx .scene .input .ClipboardContent content = new javafx .scene .input .ClipboardContent ();
81+ content .putString (errorTitle + "\n \n " + errorMessage + "\n \n " + stackTrace );
82+ clipboard .setContent (content );
83+ });
84+
85+ Button returnButton = new Button ("返回主界面" );
86+ returnButton .setOnAction (e -> {
87+ if (returnHandler != null ) {
88+ returnHandler .handle (e );
89+ }
90+ });
91+
92+ HBox buttonBox = new HBox (10 , copyButton , returnButton );
93+ buttonBox .setAlignment (Pos .CENTER_RIGHT );
94+
95+ // 组装界面
96+ VBox topBox = new VBox (10 , title , errorTitleLabel , errorMessageLabel );
97+
98+ VBox centerBox = new VBox (10 );
99+ centerBox .getChildren ().addAll (
100+ new Label ("详细错误信息:" ),
101+ scrollPane
102+ );
103+
104+ setTop (topBox );
105+ setCenter (centerBox );
106+ setBottom (buttonBox );
107+ }
108+
109+ private String getStackTraceAsString (Throwable throwable ) {
110+ if (throwable == null ) {
111+ return "无堆栈跟踪信息" ;
112+ }
113+
114+ java .io .StringWriter sw = new java .io .StringWriter ();
115+ java .io .PrintWriter pw = new java .io .PrintWriter (sw );
116+ throwable .printStackTrace (pw );
117+ return sw .toString ();
118+ }
119+ }
0 commit comments