2014-02-26 64 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