2014-02-26 95 views
0

我有這個GUI程序,我試圖基本上覆制Windows CMD。由於我在這個程序中有很多功能,我決定將部分代碼放在不同的類中。但它沒有迴應。Java沒有從其他類的響應

 if(command.size()<2 && 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(); } 
    } 
} 

這裏是打印();在主類...

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中,它就可以正常工作;但是這將會是一個問題,因爲一些函數可能會有數百行代碼,因此將所有內容都放到主類中。

程序死機時不會產生錯誤。

+0

您可以在pastebin.com上添加終端類的代碼嗎?您是否嘗試過使用調試器? – neowulf33

+0

@ neowulf33 http://pastebin.com/7Z1WuMJF – Arc

回答

1

在前面的代碼片段中,代碼試圖創建新的終端,而不是使用現有的終端。

試試這個:

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

+0

感謝您的回答,只是一個簡單的問題。這是在一個單獨的類上,還是在主類的terminal.java中? – Arc

+0

打印方法將在終端類中。 – neowulf33

+0

'在旁註中,儘可能避免靜電。一般來說,這是不好的做法。' 如何?靜態是編程的關鍵部分... – Qix