-2

实际上,我已经创建了一个摆动面板,它接收来自用户的输入,例如 sourceno、destinationno、ttl,并基于这些数据,我将应用我的纯随机传播算法 (PRP) 来找出网络中遍历的路由节点。下面的代码 (PRP.java) 将在按下摆动面板中的提交按钮时在命令提示符下打印遍历的路由节点。

我需要修改代码,以便在按下提交按钮时,生成的遍历路线应打印在提交按钮下方同一面板中的文本框中,而不是在命令提示符下

请建议修改代码.....

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;

public class PRP extends JFrame
{


JTextField srcno;
JTextField destno;
JTextField ttl;
JTextField datapayload;
JLabel srcnode;
JLabel destnode;
JLabel TTL;
JLabel Datapayload;
JButton submit;
JTextArea textFieldName;


  public PRP()
  {

    PRPLayout customLayout = new PRPLayout();

    getContentPane().setFont(new Font("Helvetica", Font.PLAIN, 12));
    getContentPane().setLayout(customLayout);

    srcnode = new JLabel("Enter source node:");
    getContentPane().add(srcnode);

    srcno = new JTextField("number");
    getContentPane().add(srcno);

    destnode = new JLabel("Enter destination node:");
    getContentPane().add(destnode);

    destno = new JTextField("number");
    getContentPane().add(destno);

    TTL = new JLabel("Enter TTL:");
    getContentPane().add(TTL);

    ttl = new JTextField("number");
    getContentPane().add(ttl);

     Datapayload = new JLabel("Enter DataPayload:");
    getContentPane().add(Datapayload);

    datapayload = new JTextField("abcd...");
    getContentPane().add(datapayload);

    submit = new JButton("submit");
    getContentPane().add(submit);

    textFieldName = new JTextArea("PRP ROUTING:",80,10);
    textFieldName.setEditable( false );
    JScrollPane scroll = new JScrollPane(textFieldName) ;
    scroll.setBounds( 10, 60, 225, 150 );
    getContentPane().add( scroll );

    submit.addActionListener(new ActionListener()
                                         {
                                            public void actionPerformed(ActionEvent e) 
                                            {

                                              String[]  prpdata = new String[3];
                                              prpdata[0]=srcno.getText();
                                              prpdata[1]=destno.getText();
                                              prpdata[2]=ttl.getText();
                                              try{ 
                                              PRP pr = new PRP();
                                              pr.PRPRoute(prpdata); }
                                              catch(Exception ez){ textFieldName.append("exception in caller method"); }


                                             }
                                           });

    setSize(getPreferredSize());

    }

    public void PRPRoute(String argus[]) throws ClassNotFoundException,SQLException
    {

     String url="jdbc:oracle:thin:@localhost:1521:xe";
     String u="proj";
     String p="bade";
     String src=argus[0];
     String dest=argus[1];
     int ttl=Integer.parseInt(argus[2]);
     Class.forName("oracle.jdbc.driver.OracleDriver");
     Connection con=DriverManager.getConnection(url,u,p);
     if(con==null)
     {
       textFieldName.append("not connected to oracle");
       return;
      }

     textFieldName.append("connected to oracle");

     String query= "select distinct RFrom,RTo from router where RFrom='"+src+"'"+"or "+ "RTo='"+src+"'";

     Statement st=con.createStatement();
     ResultSet rs=st.executeQuery(query);

     ResultSetMetaData rsmd = rs.getMetaData();

     int j=-1;

     String[]  randata = new String[30];
     textFieldName.append( "\n\t"+"Neighbors of " + src+"::");  

     while( rs.next() )
     {  
       j++;
       for (int i = 1; i <= rsmd.getColumnCount(); i++)
      {

        String ss=rs.getString( i );
        if(!(ss.equals(src)))
        {
           textFieldName.append( "\t" + ss );
           randata[j]=ss ;  
        }
      }

      textFieldName.append( "\n\n" );   
     }
     int k=0;
     textFieldName.append("\n"+"Checking for 1-hop neighbours. . . .");

     for(int i=0;i<=j;i++)
     {
        if(randata[i].equals(dest))
        textFieldName.append("1-hop neighbour found");
        textFieldName.append( "\t" + "Routing followed:"+src+"--->" +randata[i]);
        textFieldName.append("\n\t successfully routed!");
        k=i;
        break;
      }
   }

   if(!(randata[k].equals(dest)))
   {
    textFieldName.append("Choosen dispersive PRP strategy!!!");
    textFieldName.append( "\t" + "Routing followed:"+src+"--->" );
    Random r=new Random();
    if(ttl<=0) { 
              textFieldName.append("MINHOP ROUTING STARTED");

           }
     else {
          ttl--;
          String next=randata[r.nextInt(j)];
          textFieldName.append(next);
          if(next.equals(dest)) { textFieldName.append("\n\t successfully routed!"); }
          else{
               String[] indata = new String[3];
               indata[0]=next;
               indata[1]=dest;
               indata[2]=Integer.toString(ttl);
               try{ PRPRouteout pr=new PRPRouteout(indata); }
               catch(Exception ezl){ textFieldName.append("exception in PRPRouteut caller method"); }


             }
         }
     }


    }




 public static void main(String args[])
 {
    PRP window = new PRP();

    window.setTitle("PRP");
    window.pack();
    window.show();
 }
}

class PRPLayout implements LayoutManager
{
  public PRPLayout() {  }
  public void addLayoutComponent(String name, Component comp) {   }
  public void removeLayoutComponent(Component comp) {  }
  public Dimension preferredLayoutSize(Container parent)
 {
    Dimension dim = new Dimension(0, 0);
    Insets insets = parent.getInsets();
    dim.width = 320 + insets.left + insets.right;
    dim.height = 240 + insets.top + insets.bottom;
     return dim;
 }

 public Dimension minimumLayoutSize(Container parent)
 {
    Dimension dim = new Dimension(0, 0);
    return dim;
 }
 public void layoutContainer(Container parent) 
 {
    Insets insets = parent.getInsets();
    Component c;
    c = parent.getComponent(0);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+8,172,26);}
    c = parent.getComponent(1);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+8,172,26);}
    c = parent.getComponent(2);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+40,172,26);}
    c = parent.getComponent(3);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+40,172,26);}
    c = parent.getComponent(4);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+72,172,26);}
    c = parent.getComponent(5);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+72,172,26);}
    c = parent.getComponent(6);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+112,172,26);}
    c = parent.getComponent(7);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+112,172,26);}
    c = parent.getComponent(8);
    if (c.isVisible()) {c.setBounds(insets.left+230,insets.top+152,142,26);}
    c = parent.getComponent(9);
    if (c.isVisible()) {c.setBounds(insets.left+90,insets.top+200,472,268);}
 }
}
4

2 回答 2

2

如果我正确理解了您的问题,请尝试以下操作:

使用textFieldName.setText();代替System.out.println();

于 2013-03-30T11:11:43.730 回答
0

I dont know about your code. But i know the way which we can use the Ping or trace route... and take the result into .txt file. Write your command like that ping 192.168.1.1 -t > filename.txt

mind if your use this too To delay between pings and record the time of each you can do some scripting.

@ECHO OFF :LOOPSTART time /T ping xxx.xxx.xxx.xxx -n 4 >> filename.txt ping yyy.yyy.yyy.yyy -n 4 >> filename.txt sleep -m 3000 GOTO LOOPSTART

it will create a File in root directory. and it will print all the results untill your exit the command.

于 2013-03-30T12:41:05.873 回答