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

时间:2014-02-27 18:03:21

标签: java multithreading class user-interface void

只是抬头,这个问题延伸到我之前的Java no response from other classes。我在那里得到的答案我们很棒,但我遇到了新的问题;所以这是一个完全不同的问题。

就像我之前说过的那样,我正在创建一个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
    }

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

其他任何事都让我知道。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我的语法再一次让我感到困惑......看

        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();
在分割文本字段内的任何内容之前,

行重置为空白。因此,当我通过循环传递命令链表时,它没有任何内容。

通过移动两行代码来修复......

        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

叹息我感到愚蠢