我一直试图在JAVA-JESS做一棵树,但我对JESS的知识非常有限。由于我在编程方面表现不佳,我已经从不同的网站获得了帮助,并尝试在JAVA中编写该程序。任何人都可以帮我在JAVA.Jhan中提前在JESS编写程序。
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.io.File;
import java.io.IOException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TreeView extends JPanel implements ActionListener {
private JTree tree;
JButton openButton, saveButton;
JFileChooser fc;
// Optionally set the look and feel.
private static boolean useSystemLookAndFeel = false;
public TreeView() {
super(new GridLayout(1, 1));
// Create a tree that allows one selection at a time.
tree = new JTree();
tree.getSelectionModel().setSelectionMode(
TreeSelectionModel.SINGLE_TREE_SELECTION);
// Create the scroll pane and add the tree to it.
JScrollPane treeView = new JScrollPane(tree);
add(treeView, BorderLayout.LINE_START);
Dimension minimumSize = new Dimension(10, 0);
treeView.setMinimumSize(minimumSize);
fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("xml",
"XML");
fc.setFileFilter(filter);
openButton = new JButton("Open a File");
openButton.addActionListener(this);
// For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(); // use FlowLayout
openButton.setPreferredSize(new Dimension(1, 1));
// Add the buttons and the log to this panel.
add(openButton, BorderLayout.LINE_END);
}
public void actionPerformed(ActionEvent e) {
// Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fc.showOpenDialog(TreeView.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
regenerateTree(fc.getSelectedFile());
}
// Handle save button action.
} else if (e.getSource() == saveButton) {
int returnVal = fc.showSaveDialog(TreeView.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
}
}
}
private void regenerateTree(File file) {
DistributionSystem ds = DSParser.parse(file);
// Create the nodes.
DefaultMutableTreeNode top = new DefaultMutableTreeNode(file.getName());
createNodes(top, ds);
}
private void createNodes(DefaultMutableTreeNode top, DistributionSystem ds) {
DefaultMutableTreeNode category = null;
category = new DefaultMutableTreeNode("Voltage Levels");
top.add(category);
Collection<VoltageLevel> voltages = ds.getVoltages().values();
for (Iterator iterator = voltages.iterator(); iterator.hasNext();) {
// voltage levels
VoltageLevel vl = (VoltageLevel) iterator.next();
DefaultMutableTreeNode voltageLevel = new DefaultMutableTreeNode(
vl.getId());
category.add(voltageLevel);
// workstations
Collection<Workstation> workstations = vl.getWorkstations();
for (Iterator iterator2 = workstations.iterator(); iterator2
.hasNext();) {
Workstation w = (Workstation) iterator2.next();
DefaultMutableTreeNode workstationNode = new DefaultMutableTreeNode(
w.getId());
voltageLevel.add(workstationNode);
// bays
Collection<Bay> bays = w.getBays();
for (Iterator iterator3 = bays.iterator(); iterator3.hasNext();) {
Bay bay = (Bay) iterator3.next();
DefaultMutableTreeNode bayNode = new DefaultMutableTreeNode(
bay.getId());
workstationNode.add(bayNode);
// Bay Info
bayNode.add(new DefaultMutableTreeNode(bay
.getVoltageTransformer()));
bayNode.add(new DefaultMutableTreeNode(bay
.getCurrentTransformer()));
}
}
}
this.tree.setModel(new JTree(top).getModel());
}
/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event dispatch thread.
*/
private static void createAndShowGUI() {
if (useSystemLookAndFeel) {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Couldn't use system look and feel.");
}
}
// Create and set up the window.
JFrame frame = new JFrame("Network visualization");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Add content to the window.
frame.add(new TreeView());
// Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event dispatch thread:
// creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}