Java没有其他类的响应

我有这个GUI程序,我试图基本上复制Windows CMD。 由于我在这个程序中有很多function,所以我决定将部分代码放在不同的类中。 但它没有回应。

if(command.size()0) { switch(command.get(0)) { case "dt": getDateTime a = new getDateTime(); a.Start(); break; // other case(s) down below } } 

这是geDateTime类

 public class getDateTime { public static void Start() { Terminal t = new Terminal(); try { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = new Date(); String s = dateFormat.format(date).toString(); t.print(s); }catch(Exception e){ e.printStackTrace(); } } } 

这是print(); 在主类中无效……

 public static void print(String s) { Color c = Color.WHITE; // prints white text to JFrame Style style = output.addStyle("Style", null); StyleConstants.setForeground(style, c); try{ document.insertString(document.getLength(), s, style); }catch(Exception e){e.printStackTrace();} } 

现在,当我输入访问getDateTime类的命令时,程序冻结,我无法输入任何内容。 但是,如果我只是将getDateTime类放入主类中的void中,它可以正常工作; 但是将一切都放入主类是一个问题,因为一些函数可能有数百行代码。

程序冻结时不会产生错误。

在前面的代码片段中,代码试图创建一个新的终端,而不是使用现有的终端。

尝试这个:

 private static void print() { DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date date = new Date(); String s = dateFormat.format(date).toString(); print(s); } 

在访问方法中:

 case "dt": print(); break; 

更新:在旁注,尽可能避免静电。 一般来说,这是不好的做法。 请参阅https://stackoverflow.com/a/7026563/1216965