2013-03-30 24 views
-2

其實我已經創建了一個swing用戶面板,它接受來自用戶的輸入信息,如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);} 
} 
} 
+1

這是一個很好的問題,但例子太長了。 – Val

+0

由於其瘋狂的格式化,我無法讀取您的代碼。考慮解決這個問題。 –

+0

改變代碼和格式化先生....看看它曾經@Hovercraft全部鰻魚 – sbade

回答

2

如果我明白你的問題正確,然後試試這個:

使用textFieldName.setText();,而不是System.out.println();

+0

我改變了我的代碼,通過替換System.out.println();與textFieldName.append(); ...沒有錯誤,但textearea只是空白,沒有任何結果.... @bhushan – sbade

+0

@ user2224922'JTextField jtf = new JTextField(); jtf.setText(「Bhushan」);'這是需要做的,首先創建一個文本字段對象,然後使用'setText()'方法在文本字段中設置文本 – Bhushan

0

我不知道你的代碼。但是我知道我們可以使用Ping或跟蹤路由的方式......並將結果導入.txt文件。寫下你的命令吧 ping 192.168.1.1 -t> filename.txt

請記住,如果你也使用這個 要在ping之間延遲並記錄每個腳本的時間,你可以做一些腳本。

@ECHO OFF :環路啓動 時間/ T 平XXX.XXX.XXX.XXX -n 4 >> FILENAME.TXT 平yyy.yyy.yyy.yyy -n 4 >> FILENAME.TXT 睡眠-m 3000 GOTO LOOPSTART

它會在根目錄下創建一個文件。它將打印所有結果,直到退出命令。