简体   繁体   English

显示从文本文件到jtextfield的节点的可能路径

[英]Displaying the possible paths of nodes from textfile into jtextfield java swing

I am trying to find all the possible paths of nodes by getting the start and destination node as input. 我试图通过获取起始节点和目标节点作为输入来查找节点的所有可能路径。

I am reading all possible paths of all nodes from a text file and then filter the possible paths. 我正在从文本文件中读取所有节点的所有可能路径,然后过滤可能的路径。

I am getting the output when using System.out.println function. 使用System.out.println函数时得到输出。 But I'm having trouble when setting the output to a JTextArea . 但是在将输出设置为JTextArea时遇到麻烦。

I have added my code below. 我在下面添加了我的代码。

Graph.java Graph.java

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

public class Graph {
    private Map<String, LinkedHashSet<String>> map = new HashMap();

    public void addEdge(String node1, String node2) {
        LinkedHashSet<String> adjacent = map.get(node1);
        if(adjacent==null) {
            adjacent = new LinkedHashSet();
            map.put(node1, adjacent);
        }
        adjacent.add(node2);
    }

    public void addTwoWayVertex(String node1, String node2) {
        addEdge(node1, node2);
        addEdge(node2, node1);
    }

    public boolean isConnected(String node1, String node2) {
        Set adjacent = map.get(node1);
        if(adjacent==null) {
            return false;
        }
        return adjacent.contains(node2);
    }

    public LinkedList<String> adjacentNodes(String last) {
        LinkedHashSet<String> adjacent = map.get(last);
        if(adjacent==null) {
            return new LinkedList();
        }
        return new LinkedList<String>(adjacent);
    }
}

Search.java Search.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Search extends JFrame{

    public static Graph graph;

    public JFrame jPanel;
    public JLabel headingLabel,startLabel,endLabel,possiblePathLabel;
    public JTextField startField,endField;
    public JTextArea possiblePathArea;
    public JButton findButton;
    JScrollPane jsp;

    private static String START = "";
    private static String END = "";

    List<String> stringList=new ArrayList<String>();       

    public Search(){
        jPanel=new JFrame("Optimal Path Computation");
        jPanel.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 
        headingLabel=new JLabel("Optimal Path Computation");
        startLabel=new JLabel("Source Node");
        endLabel=new JLabel("Destination Node");
        possiblePathLabel=new JLabel("Possible Paths");
        startField=new JTextField();
        endField=new JTextField();
        possiblePathArea=new JTextArea(16,58);
        findButton=new JButton("Find possible path");
    }

    public void launchFrame(){
        jPanel.setLayout(null);
        jPanel.setVisible(true);
        jPanel.setExtendedState(JFrame.MAXIMIZED_BOTH);

        jPanel.add(headingLabel);
        headingLabel.setSize(270,50);
        headingLabel.setLocation(550, 100);
        headingLabel.setHorizontalAlignment(JLabel.CENTER);

        jPanel.add(startLabel);
        startLabel.setSize(270,50);
        startLabel.setLocation(375, 150);
        startLabel.setHorizontalAlignment(JLabel.CENTER);

        jPanel.add(endLabel);
        endLabel.setSize(270,50);
        endLabel.setLocation(375, 225);
        endLabel.setHorizontalAlignment(JLabel.CENTER);

        jPanel.add(startField);
        startField.setSize(270,30);
        startField.setLocation(775, 150);
        startField.setHorizontalAlignment(JLabel.CENTER);

        jPanel.add(endField);
        endField.setSize(270,30);
        endField.setLocation(775, 225);
        endField.setHorizontalAlignment(JLabel.CENTER);

        jPanel.add(findButton);
        findButton.setSize(170,30);
        findButton.setLocation(600, 325);
        findButton.setHorizontalAlignment(JLabel.CENTER);
        findButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                 START=startField.getText();
                 END=endField.getText();

                 shortFunc();
            }
        });

        jPanel.add(possiblePathLabel);
        possiblePathLabel.setSize(170,30);
        possiblePathLabel.setLocation(600, 375);
        possiblePathLabel.setHorizontalAlignment(JLabel.CENTER);

        jPanel.add(possiblePathArea);
        possiblePathArea.setSize(300, 200);
        possiblePathArea.setLocation(600, 425);
    }

    public static void main(String[] args) {
        // this graph is directional

        Search ss=new Search();
        ss.launchFrame();

        BufferedReader br=null;
        String line="";
        String eachLine="";
        List<String> list = new ArrayList<String>();

        graph = new Graph();
        try{
            br=new BufferedReader(new FileReader("D://Dataset.txt"));

            while(line!=null){
                   list.add(line);
                  line=br.readLine();
            }
            String[] stringArr = list.toArray(new String[0]);
            for(int i=1;i<=stringArr.length-1;i++){

                String eachLinesArray[]=stringArr[i].split(",");
                graph.addEdge(eachLinesArray[0], eachLinesArray[1]);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }   
    }

    public static  void shortFunc(){
        LinkedList<String> visited = new LinkedList();
        visited.add(START);
        new Search().breadthFirst(graph, visited);
    }

    private void breadthFirst(Graph graph, LinkedList<String> visited) {
        LinkedList<String> nodes = graph.adjacentNodes(visited.getLast());
        // examine adjacent nodes
        for (String node : nodes) {
            if (visited.contains(node)) {
                continue;
            }
            if (node.equals(END)) {
                visited.add(node);
                printPath(visited);
                visited.removeLast();
                break;
            }
        }
        // in breadth-first, recursion needs to come after visiting adjacent nodes
        for (String node : nodes) {
            if (visited.contains(node) || node.equals(END)) {
                continue;
            }
            visited.addLast(node);
            breadthFirst(graph, visited);
            visited.removeLast();
        }
    }

    private void printPath(LinkedList<String> visited) {

        for (String node : visited) {

            possiblePathArea.append(node+"\n");

            System.out.print(node);
            System.out.print(" ");
        }

       System.out.println();
    }      
}

Output using System.out.println() when getting start and end node as input from textfields: 从文本字段获取开始和结束节点作为输入时,使用System.out.println()进行输出:

B E 
B A C E 
B A C F E 
B F E 
B F C E

The problem is related to 问题与

public static Graph graph;

and manifests here 并显示在这里

public static  void shortFunc(){
    LinkedList<String> visited = new LinkedList();
    visited.add(START);
    new Search().breadthFirst(graph, visited);
 }

Basically, you are creating a brand new instance of Search which has absolutely nothing to do with the window that is visible on the screen. 基本上,您将创建一个全新的Search实例,该实例与屏幕上可见的窗口完全没有关系。

static is not your friend. static不是您的朋友。

Start by removing the static decleration against Graph and require that Search be provided with an instance of Graph as part of it's initialisation... 首先删除针对Graphstatic删除,并要求Search附带Graph实例作为其初始化的一部分...

public class Search extends JFrame {

    private Graph graph;
    //...

    public Search(Graph graph) {
        this.graph = graph;

Next, change your main method to load the Graph first and then the window... 接下来,更改您的main方法以先加载Graph ,然后再加载窗口...

public static void main(String[] args) {
    // this graph is directional

    BufferedReader br = null;
    String line = "";
    String eachLine = "";
    List<String> list = new ArrayList<String>();

    Graph graph = new Graph();
    try {
        br = new BufferedReader(new FileReader("Data.txt"));

        while (line != null) {
            list.add(line);
            line = br.readLine();
        }
        String[] stringArr = list.toArray(new String[0]);
        for (int i = 1; i <= stringArr.length - 1; i++) {

            String eachLinesArray[] = stringArr[i].split(",");
            graph.addEdge(eachLinesArray[0], eachLinesArray[1]);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    Search ss = new Search(graph);
    ss.launchFrame();

}

Next change shortFunc to be a instance method... 接下来将shortFunc更改为实例方法...

public void shortFunc() {
    LinkedList<String> visited = new LinkedList();
    visited.add(START);
    breadthFirst(graph, visited);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM