gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:46:13 24 4
gpt4 key购买 nike

我试图通过获取起始节点和目标节点作为输入来查找节点的所有可能路径。

我正在从文本文件中读取所有节点的所有可能路径,然后过滤可能的路径。

我在使用 System.out.println 函数时获得输出。但我在将输出设置为 JTextArea 时遇到了麻烦。

我在下面添加了我的代码。

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

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

从文本字段获取开始和结束节点作为输入时使用 System.out.println() 进行输出:

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

最佳答案

该问题与

有关
public static Graph graph;

并在此处体现

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

基本上,您正在创建一个全新的 Search 实例,它与屏幕上可见的窗口完全无关。

static 不是你的 friend 。

首先删除针对 Graphstatic 声明,并要求为 Search 提供 Graph 实例作为其初始化的一部分...

public class Search extends JFrame {

private Graph graph;
//...

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

接下来,更改您的 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();

}

接下来将 shortFunc 更改为实例方法...

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

关于java - 显示节点从文本文件到 jtextfield java swing 的可能路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29641187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com