Java没有来自其他类的响应 [英] Java no response from other classes

查看:84
本文介绍了Java没有来自其他类的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个GUI程序,我试图基本上复制Windows CMD。由于我在这个程序中有很多功能,我决定把部分代码放在不同的类中。但它不响应。

  if(command.size()< 2&& command.size > 0){
switch(command.get(0)){
casedt:
getDateTime a = new getDateTime();
a.Start();
break;
//下面的其他情况
}
}

这里是geDateTime类

  public class getDateTime {
public static void Start(){
终端t =新终端();
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(); }
}
}

void in the main class ...

  public static void print(String s){
Color c = Color。白色; //将白色文本打印到JFrame
样式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);
}

在访问方法中:

  casedt:
print();
break;

更新:在旁注中,尽量避免静态。一般来说,这是坏的做法。请参见 http://stackoverflow.com/a/7026563/1216965


I have this GUI program where I'm trying to basically copy windows CMD. Since I have lots of features in this program, I decided to put parts of the code in different classes. But it doesn't respond.

       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
            }
        }

Here is the geDateTime class

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(); }
    }
}

Here is the print(); void in the main class...

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();}
    }

Now when I enter the command for accessing the getDateTime class, the program freezes and I can't input anything. HOWEVER, if I just put the getDateTime class into a void inside the main class it works fine; but this would be a problem to just put everything into the main class since some function(s) could have hundreds of line of code.

No errors are produced when the program freezes.

解决方案

In the code snippet that you have earlier, the code was trying to create a new Terminal rather than using the existing one.

Try this:

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);
}

In the access method:

case "dt":
    print();
    break;

Update: On a side note, try to avoid static if at all possible. Generally speaking, it's bad practice. See http://stackoverflow.com/a/7026563/1216965

这篇关于Java没有来自其他类的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆