如何将打印输出到JTextField?

时间:2019-01-02 02:04:05

标签: java swing jtextfield

我对如何将打印输出到JTextField感到困惑,我尝试使用setText,但没有在文本字段中显示任何内容。

这是呼叫输出方法的方法:

public void eksekusi(){
        PetaKalselForm search = new PetaKalselForm();
        search.intialize();     
        String searchType = tipe.getSelectedItem().toString();
        String sourceCity = awal.getSelectedItem().toString();
        String destinationCity = tujuan.getSelectedItem().toString();

        if (ASTAR.equals(searchType)) {
            search.astar(sourceCity, destinationCity);
        } else if (GREEDY.equals(searchType)) {
            search.greedy(sourceCity, destinationCity);
         } else if (DYNAMIC.equals(searchType)) {
            search.dynamic(sourceCity, destinationCity);
        }
}

这是输出方法:

public void greedy(String sourceCity, String destinationCity) {
    initComponents();
    Queue<PetaKalselForm.Node> queue = new PriorityQueue<PetaKalselForm.Node>();

    List<String> visitedList = new ArrayList<String>();
    queue.add(new PetaKalselForm.Node(sourceCity, 0, 0));
    int totalNodesExpanded = 0;

    while(!queue.isEmpty()) {
        // Check if the queue is empty
        PetaKalselForm.Node currentNode = queue.remove();
        // Removing the current node from the queue
        if(goalChecker(currentNode.getPath(), destinationCity)) {
            // To check if the goal is achieved
            String totalnode = String.valueOf(totalNodesExpanded);

            logOutput("");


            logOutput("Total Nodes Expanded by Greedy = " + totalnode);
            logOutput("Path generated by Greedy: " + currentNode.getPath());
            logOutput("Path length generated by Greedy: " + getPathLength(currentNode.getPath()));
            int totalCost = getPathCost(currentNode.getPath());
            logOutput("Cost of Greedy: " + totalCost);
            return;
        } else {
            String currentCity = getCurrentCity(currentNode.getPath());
            // Get the expanded list of cities from the current source
            if(totalNodesExpanded != 0) {
                System.out.print(", ");
            } else {
                System.out.print("Nodes Expanded are : ");
            }
            totalNodesExpanded++;
            System.out.print(currentCity);
            visitedList.add(currentCity);
            List<PetaKalselForm.Node> possibleMoves = moveGenerator(currentCity);
            // Defining the move generator
            for (PetaKalselForm.Node possibleMove : possibleMoves) {
                if(!visitedList.contains(possibleMove.getPath())) {
                    // Calculate the heuristic value to add the city in the queue
                    String newPath = currentNode.getPath() + PATH_SEPERATOR + possibleMove.getPath();
                    double newHeuristicCost = calcHeuristicCost(possibleMove.getPath(), destinationCity);
                    PetaKalselForm.Node node = new PetaKalselForm.Node(newPath, 0, newHeuristicCost);
                    if(queue.contains(node)) {
                        updateQueueNode(queue, node);
                    } else {
                        queue.add(node);
                    }
                }
            }
        }
    }
    logOutput("\nNo path found for Greedy");
}

我尝试将logOutput更改为jTextfiel1.setText,但是没有用。

0 个答案:

没有答案