www.pudn.com > 距离矢量路由模拟.rar > RooterFrame1.java~228~
package rooter; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.borland.jbcl.layout.*; import javax.swing.border.*; import javax.swing.BorderFactory; /** *Title:
*Description:
*Copyright: Copyright (c) 2005
*Company:
* @author not attributable * @version 1.0 */ public class RooterFrame1 extends JFrame { /////////////////////////////////////////////////// private final int MAX_VERTS = 20; private final int INFINITY = 1000000; private Vertex vertexList[]; // list of vertices private int adjMat[][]; // adjacency matrix private int nVerts; // current number of vertices private int nTree; // number of verts in tree private DistPar sPath[]; // array for shortest-path data private int currentVert; // current vertex private int startToCurrent; // distance to currentVert //////////////////////////////////////////////////// ImageIcon image1 = new ImageIcon("/router.png"); JPanel contentPane; XYLayout xYLayout1 = new XYLayout(); JPanel jPanel1 = new JPanel(); TitledBorder titledBorder1; JLabel jLabel1 = new JLabel(); JButton router0 = new JButton(); JButton router1 = new JButton(); JButton router2 = new JButton(); JButton router3 = new JButton(); JButton router4 = new JButton(); TitledBorder titledBorder2; TitledBorder titledBorder3; JPanel jPanel4 = new JPanel(); TitledBorder titledBorder4; JLabel jLabel2 = new JLabel(); XYLayout xYLayout3 = new XYLayout(); JTextField sendJTextField = new JTextField(); JLabel jLabel3 = new JLabel(); JButton send = new JButton(); JScrollPane jScrollPane3 = new JScrollPane(); JScrollPane jScrollPane4 = new JScrollPane(); JLabel jLabel5 = new JLabel(); JTextArea jTextArea2 = new JTextArea(); JTextArea jTextArea3 = new JTextArea(); JButton linkButton = new JButton(); JTextArea jTextArea1 = new JTextArea(); FlowLayout flowLayout1 = new FlowLayout(); //Construct the frame public RooterFrame1() { ////////////////////////////////////////////////////////// vertexList = new Vertex[MAX_VERTS]; // adjacency matrix adjMat = new int[MAX_VERTS][MAX_VERTS]; nVerts = 0; nTree = 0; for (int j = 0; j < MAX_VERTS; j++) // set adjacency for (int k = 0; k < MAX_VERTS; k++) // matrix adjMat[j][k] = INFINITY; // to infinity sPath = new DistPar[MAX_VERTS]; // shortest paths ////////////////////////////////////////////////////////// enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } //Component initialization private void jbInit() throws Exception { contentPane = (JPanel) this.getContentPane(); jLabel2.setFont(new java.awt.Font("Dialog", 1, 11)); jLabel2.setForeground(Color.blue); jLabel2.setBorder(BorderFactory.createRaisedBevelBorder()); jLabel2.setHorizontalAlignment(SwingConstants.CENTER); jLabel2.setHorizontalTextPosition(SwingConstants.CENTER); jLabel2.setText("发送给"); titledBorder1 = new TitledBorder("模拟路由器"); titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(178, 178, 178)),"链路状态信息"); titledBorder3 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(178, 178, 178)),"路由表"); titledBorder4 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(148, 145, 140)),"路由信息"); contentPane.setLayout(xYLayout1); this.getContentPane().setBackground(UIManager.getColor( "InternalFrame.inactiveTitleBackground")); this.setJMenuBar(null); this.setSize(new Dimension(550,400)); this.setTitle("链路状态路由"); this.setVisible(true); contentPane.setForeground(Color.black); jPanel1.setBackground(SystemColor.inactiveCaption); jPanel1.setBorder(titledBorder1); jPanel1.setLayout(flowLayout1); jLabel1.setBackground(Color.pink); jLabel1.setFont(new java.awt.Font("Dialog", 0, 15)); jLabel1.setForeground(Color.blue); jLabel1.setBorder(BorderFactory.createRaisedBevelBorder()); jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jLabel1.setHorizontalTextPosition(SwingConstants.CENTER); jLabel1.setText("链路状态路由实验模拟"); jLabel1.setVerticalAlignment(SwingConstants.CENTER); jLabel1.setVerticalTextPosition(SwingConstants.CENTER); router0.setIcon(image1); router0.setText("Router0"); router0.addActionListener(new RooterFrame1_router0_actionAdapter(this)); router1.setText("Router1"); router1.addActionListener(new RooterFrame1_router1_actionAdapter(this)); router2.setSelectedIcon(null); router2.setText("Router2"); router2.addActionListener(new RooterFrame1_router2_actionAdapter(this)); router3.setText("Router3"); router3.addActionListener(new RooterFrame1_router3_actionAdapter(this)); router4.setText("Router4"); router4.addActionListener(new RooterFrame1_router4_actionAdapter(this)); jPanel4.setBorder(titledBorder4); jPanel4.setLayout(xYLayout3); sendJTextField.setText(""); jLabel3.setFont(new java.awt.Font("Dialog", 1, 11)); jLabel3.setForeground(Color.blue); jLabel3.setBorder(BorderFactory.createRaisedBevelBorder()); jLabel3.setHorizontalAlignment(SwingConstants.CENTER); jLabel3.setHorizontalTextPosition(SwingConstants.CENTER); jLabel3.setText("号路由器"); send.setFont(new java.awt.Font("Dialog", 1, 13)); send.setForeground(Color.blue); send.setActionCommand("发送信息"); send.setText("发送信息"); send.addActionListener(new RooterFrame1_send_actionAdapter(this)); jLabel5.setFont(new java.awt.Font("Dialog", 1, 13)); jLabel5.setForeground(Color.blue); jLabel5.setBorder(BorderFactory.createRaisedBevelBorder()); jLabel5.setHorizontalAlignment(SwingConstants.CENTER); jLabel5.setHorizontalTextPosition(SwingConstants.CENTER); jLabel5.setIconTextGap(4); jLabel5.setText("路由表"); jTextArea2.setText(""); jScrollPane3.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); jTextArea3.setText(""); jScrollPane4.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); jScrollPane4.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); linkButton.setFont(new java.awt.Font("Dialog", 1, 13)); linkButton.setForeground(Color.blue); linkButton.setText("链路信息"); contentPane.add(linkButton, new XYConstraints(22, 83, 107, -1)); jPanel4.add(sendJTextField, new XYConstraints(48, 1, 28, 26)); contentPane.add(jLabel1, new XYConstraints(64, 1, 394, 30)); contentPane.add(jLabel5, new XYConstraints(277, 84, 61, 26)); contentPane.add(jScrollPane4, new XYConstraints(277, 113, 245, 124)); jScrollPane4.getViewport().add(jTextArea3, null); contentPane.add(jScrollPane3, new XYConstraints(20, 112, 240, 125)); jScrollPane3.getViewport().add(jTextArea2, null); jPanel4.add(jTextArea1, new XYConstraints(6, 31, 481, 41)); jPanel4.add(jLabel2, new XYConstraints(7, 0, 41, 27)); jPanel4.add(jLabel3, new XYConstraints(78, 0, 69, 28)); jPanel4.add(send, new XYConstraints(155, 0, 92, 28)); contentPane.add(jPanel1, new XYConstraints(26, 33, 502, 49)); contentPane.add(jPanel4, new XYConstraints(20, 235, 503, 105)); jPanel1.add(router0, null); jPanel1.add(router1, null); jPanel1.add(router2, null); jPanel1.add(router3, null); jPanel1.add(router4, null); } //Overridden so we can exit when window is closed protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public void router0_actionPerformed(ActionEvent e) { jTextArea2.setText(""); jTextArea2.append("线路\t" + "延迟\n"); jTextArea2.append("0->1\t" + 5 + "\n"); jTextArea2.append("0->3\t" + 8 + "\n"); //------------------------------------------------------ jTextArea3.setText(""); jTextArea3.append("目的\t下一站\t时间\n"); add(); path(0); //-------------------------------------------------------------- for(int j=0; j2\t" + "6\n"); jTextArea2.append("1->3\t" + "9\n"); jTextArea2.append("1->4\t" + "5\n"); //---------------------------------------------------------- jTextArea3.setText(""); jTextArea3.append("目的\t下一站\t时间\n"); add(); path(1); //-------------------------------------------------------------- for(int j=0; j 4\t" + "4\n"); jTextArea2.append("2->3\t" + "2\n"); jTextArea3.setText(""); jTextArea3.append("目的\t下一站\t时间\n"); add(); path(2); //-------------------------------------------------------------- for (int j = 0; j < nVerts; j++) { // display contents of sPath[] jTextArea3.append(String.valueOf(vertexList[j].label) + "\t"); char parent = vertexList[sPath[j].parentVert].label; jTextArea3.append(String.valueOf(parent) + "\t"); if (sPath[j].distance == INFINITY) jTextArea3.append("inf\t\n"); else jTextArea3.append(String.valueOf(sPath[j].distance) + "\n"); } //-------------------------------------------------------------- // cleartree(); } public void router3_actionPerformed(ActionEvent e) { jTextArea2.setText(""); jTextArea2.append("线路\t" + "延迟\n"); jTextArea2.append("3->0\t" + "8\n"); jTextArea2.append("3->1\t" + "9\n"); jTextArea2.append("3->2\t" + "2\n"); jTextArea2.append("3->4\t" + "7\n"); jTextArea3.setText(""); jTextArea3.append("目的\t下一站\t时间\n"); add(); path(3); //-------------------------------------------------------------- for (int j = 0; j < nVerts; j++) { // display contents of sPath[] jTextArea3.append(String.valueOf(vertexList[j].label) + "\t"); char parent = vertexList[sPath[j].parentVert].label; jTextArea3.append(String.valueOf(parent) + "\t"); if (sPath[j].distance == INFINITY) jTextArea3.append("inf\t\n"); else jTextArea3.append(String.valueOf(sPath[j].distance) + "\n"); } //-------------------------------------------------------------- // cleartree(); } public void router4_actionPerformed(ActionEvent e) { jTextArea2.setText(""); jTextArea2.append("线路\t" + "延迟\n"); jTextArea2.append("4->2\t" + "4\n"); jTextArea2.append("4->1\t" + "5\n"); jTextArea2.append("4->3\t" + "7\n"); jTextArea3.setText(""); jTextArea3.append("目的\t下一站\t时间\n"); add(); path(4); //-------------------------------------------------------------- for (int j = 0; j < nVerts; j++) { // display contents of sPath[] jTextArea3.append(String.valueOf(vertexList[j].label) + "\t"); char parent = vertexList[sPath[j].parentVert].label; jTextArea3.append(String.valueOf(parent) + "\t"); if (sPath[j].distance == INFINITY) jTextArea3.append("inf\t\n"); else jTextArea3.append(String.valueOf(sPath[j].distance) + "\n"); } //-------------------------------------------------------------- // cleartree(); } ////////////////////////////////////////////////////////// public void addVertex(char lab) { vertexList[nVerts++] = new Vertex(lab); } //--------------------------------------------------------------- public void addEdge(int start, int end, int weight) { adjMat[start][end] = weight; // (directed) adjMat[end][start] = weight; } // ------------------------------------------------------------- public void path(int startTree) // find all shortest paths { // int startTree = 0; // start at vertex 0 vertexList[startTree].isInTree = true; nTree = 1; // put it in tree // transfer row of distances from adjMat to sPath for(int j=0; j