資源簡介
用JAVA模擬的CSMA/CD過程,計算機網(wǎng)絡(luò)大作業(yè),其中節(jié)點設(shè)置可變更,這里面是設(shè)置兩個節(jié)點。
代碼片段和文件信息
/*
?*?To?change?this?template?choose?Tools?|?Templates
?*?and?open?the?template?in?the?editor.
?*/
package?network;
import?java.awt.*;
import?java.awt.event.*;
import?java.lang.*;
import?javax.swing.*;
class?NetWork?extends?Jframe{
?????????public?static?int?numNode;//numNode是節(jié)點的個數(shù),i用于記錄節(jié)點下標,使下面cn對象數(shù)組可以記錄節(jié)點位置
?????????private?JTextField?node=new?JTextField(10);//創(chuàng)建文本域
?????????protected?JButton?jbr=new?JButton(“->“);//創(chuàng)建按鈕
?????????JPanel?jp=new?JPanel();//創(chuàng)建一個面板
???????//??NetWork?jp2;//演示圖窗口
?????????
?????????public?NetWork(){//第一個面板的內(nèi)容
??????????jp.setLayout(new?BorderLayout(1510));
??????????jp.add(new?JLabel(“請輸入節(jié)點個數(shù):“)?BorderLayout.WEST);//創(chuàng)建標簽將標簽加入面板中
??????????jp.add(nodeBorderLayout.EAST);//將文本域加入面板中
??????????node.setHorizontalAlignment(JTextField.RIGHT);
??????????numNode=2;
??????????
??????????add(jpBorderLayout.NORTH);
??????????jp.add(jbrBorderLayout.SOUTH);//將按鈕‘-〉’加入面板中?
??????????
??????????node.addActionListener(new?ActionListener(){
??????????public?void?actionPerformed(ActionEvent?e){
????????????numNode=Integer.parseInt(node.getText());//將在node文本域中的字符串轉(zhuǎn)換成整數(shù)并存在numNode
????????????node.requestFocusInWindow();
??????????}
??????????});
??????????jbr.addActionListener(new?ActionListener(){
???????????public?void?actionPerformed(ActionEvent?e){
??????????????????jp.removeAll();
??????????????????jp.repaint();
??????????????????jp.validate();
??????????????????jp=new?CreateNode(0);
???????????}???//若按下‘->‘則轉(zhuǎn)換到創(chuàng)建節(jié)點面板中
??????????});
?????????}
?????????
?
?????????public?class?CreateNode?extends?JPanel{//創(chuàng)建節(jié)點面板?
??????????????String?name;
???????????????double?delayTimeframeTimea;//延遲時間,最短幀時間,沖突窗口
???????????????int?RnumResendframeLengthj;//隨機數(shù)R,重發(fā)次數(shù)初始化為1,最短幀長度
????????????????
??????????????JButton?colideNOcolidego;
??????????????N?jp2;
?????????????public?CreateNode(int?iint?m){
???????????????add(new?Picture(im));
?????????????}
?????????????
??????????????public?CreateNode(){}?????
??????????????JTextField[]?ne=new?JTextField[numNode];
??????????????JTextField[]?length=new?JTextField[numNode];
??????????????CreateNode[]?createN=new?CreateNode[numNode];
??????????????int?t;
??????????????public??CreateNode(int?i){
??????????????t=0;numResend=1;
?????????????jp.setLayout(new?GridLayout(41011));
?????????????for(;t ????????????{??
???????????????createN[t]=new?CreateNode();
???????????????jp.add(new?JLabel(“請輸節(jié)點名:“));//輸入節(jié)點的情況
???????????????ne[t]=new?JTextField(10);
???????????????jp.add(ne[t]);
???????????????
???????????????length[t]=new?JTextField(10);
???????????????jp.add(new?JLabel(“最小幀長度:“));
???????????????jp.add(length[t]);
??????????????
???????????????//除以10Mbps算出節(jié)點的發(fā)送最短幀時間(單位:us)
???????????????createN[t].R=(int)(1+Math.random()*100);//隨機為每個節(jié)點送一個地址值
???????????????createN[t].delayTime=0;//初始化延遲時間和重發(fā)次數(shù)
???????????????createN[t].a=51.2;//初始化重發(fā)次數(shù)和沖突窗口
????????????}????
???????????????switch(numNode)
?????????
評論
共有 條評論