Java没有来自线程或其他类的响应



请注意,这个问题是从我上一个Java扩展而来的,其他类没有响应。我在那里得到的答案很好,但我遇到了新的问题;所以这是一个完全不同的问题。

正如我之前所说的,我正在制作一个CMD副本,用户在JTextField中输入一个命令,然后输出到JScrollPane/JTextPane。

以下是每当用户按下键盘上的ENTER键时的JTextField代码。

问题是,一旦你阅读了下面的代码,代码甚至没有到达访问(命令)中的第一个调试行;无效的并没有产生错误,再一次,并没有知道发生了什么。再一次,如果我能把代码组织在不同的类中,我会很高兴的。但由于此代码只有在实际代码(如DateTime.class的代码)位于主类中时才有效;我不知道该怎么办。

public void inputFieldActionPerformed(java.awt.event.ActionEvent evt) {
print(inputField.getText()); // prints whatever the user entered in textfield to pane
inputField.setText(""); // sets textfield blank
inputField.requestFocus(); // requests textfield's focus
String[] temp = inputField.getText().split(" "); // splits whatever user entered
LinkedList<String> command = new LinkedList<>(Arrays.asList(temp)); // adds the array above into a linkedlist because I prefer them
access(command); // handles the command
}

"访问(命令);"无效的代码。

public void access(LinkedList<String> command) {
if(command.size()<2 && command.size()>0) { // goes here if user enters only a single word command
if(command.get(0).equals("dt")) {
System.out.println("thread read"); // debug to see if code even get's this far
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread started"); // another debug
DateTime dt = new DateTime();
String s = dt.Start();
print(s);
System.out.println("thread finished"); // another debug
}
}).start();
}
}
else if(command.size()>2) { // goes here if user enters a multi-word command
}
}

DateTime类。

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

打印功能。

public static void print(String s) {
Color c = Color.WHITE;
Style style = output.addStyle("Style", null);
StyleConstants.setForeground(style, c);
try{
if(!s.startsWith(" ")) {
s = " "+s;
}
if(!s.endsWith("n")) {
s = s + "n";
}
document.insertString(document.getLength(), s, style);
}catch(Exception e){e.printStackTrace();}
}

其他任何事情都告诉我。

您提到的"问题是,一旦您阅读了下面的代码,代码甚至没有到达访问(命令)中的第一个调试行"之类的大多数问题都是因为代码流由于某种原因无法到达该点。由于access()是在inputFieldActionPerformed()中调用的,因此应该在那里添加断点,并检查函数是否已执行。

我自己的语法再一次把我搞砸了。。。观看

print(inputField.getText());
inputField.setText("");
inputField.requestFocus();
String[] temp = inputField.getText().split(" ");
LinkedList<String> command = new LinkedList<>(Arrays.asList(temp));
System.out.println("reaching access");
access(command); // handles the command
System.out.println("passed access");

注意

inputField.setText("");
inputField.requestFocus();

在我拆分文本字段内的内容之前,行重置为空白。因此,当我通过循环传递命令linkedlist时,它什么都没有

通过移动两行代码修复。。。

print(inputField.getText());
String[] temp = inputField.getText().split(" ");
LinkedList<String> command = new LinkedList<>(Arrays.asList(temp));
System.out.println("reaching access");
access(command); // handles the command
System.out.println("passed access");
inputField.setText(""); // notice its at the end now
inputField.requestFocus(); // notice its at the end now

叹气我觉得很笨

最新更新