《ARQ协议模拟实现java》由会员分享,可在线阅读,更多相关《ARQ协议模拟实现java(9页珍藏版)》请在金锄头文库上搜索。
1、Timer.java 文件package common;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.SimpleDateFormat;import java.util.Date;import javax.swing.Timer;import javax.swing.JTextField;public class TimeDate now ;Timer timer;JTextField lbl;public Time( JTextField l) lbl=l; ti
2、mer = new Timer(1000, new ActionListener() public void actionPerformed(ActionEvent e) now = new Date(now.getTime() - 1000);SimpleDateFormat formatter = new SimpleDateFormat(HH:mm:ss);lbl.setText(formatter.format(now););SuppressWarnings(deprecation)public void time_start() / lbl.setText(00:00:10);now
3、 = new Date();now.setHours(0);now.setMinutes(0);now.setSeconds(10);timer.start();public void time_stop()timer.stop(); lbl.setText(00:00:10); public void time_restart();timer.stop();this.time_start();ARQ.java 文件import java.awt.*;import java.awt.event.*;import javax.swing.BoxLayout;import javax.swing.
4、JFrame;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.JScrollPane;import javax.swing.JTextField;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.ButtonGroup;import javax.swing.JTable;import javax.swing.event.DocumentEvent;import javax.swing.event.
5、DocumentListener;import javax.swing.table.*;import javax.swing.text.Document;import java.io.FileWriter;import java.io.IOException;import common.Time;public class ARQ extends JFrame private static final long serialVersionUID = -5437589209629747957L;private int NUM;private Time T;private JTextField st
6、atus;private JButton b;private JRadioButton r;DefaultTableModel m_data;JTable m_view;FileWriter f1; FileWriter f2;FileWriter f3;public ARQ() throws IOExceptionsuper(ARQ);NUM=0;Container c=getContentPane();c.setLayout(new FlowLayout(); JPanel P=new JPanel2;P0=new JPanel();P0.setLayout(new BoxLayout(P
7、0,BoxLayout.X_AXIS);c.add(P0);P1=new JPanel();c.add(P1);JPanel Q=new JPanel2; Q0=new JPanel();Q0.setLayout(new BoxLayout(Q0,BoxLayout.Y_AXIS);P0.add(Q0);Q1=new JPanel();Q1.setLayout(new BoxLayout(Q1,BoxLayout.Y_AXIS);P0.add(Q1);JPanel R=new JPanel4; R0=new JPanel();R0.setLayout(new FlowLayout(FlowLa
8、yout.LEFT);Q0.add(R0);R1=new JPanel();R1.setLayout(new FlowLayout(FlowLayout.LEFT);Q0.add(R1);R2=new JPanel();R2.setLayout(new FlowLayout(FlowLayout.LEFT);Q0.add(R2);R3=new JPanel();R3.setLayout(new FlowLayout(FlowLayout.CENTER);Q0.add(R3);/文件f1=new FileWriter(f1.txt,true);f2=new FileWriter(f2.txt,t
9、rue);f3=new FileWriter(f3.txt,true);/数据发送状态JTextField s=new JTextField(新数据,7), new JTextField(重发,7) ,new JTextField(倒计时,4), new JTextField(00:00:10,10), new JTextField(数据,4), new JTextField(,15) ;/数据发送住状态status=s;status0.setEditable(false);status1.setEditable(false);status2.setEditable(false);status
10、4.setEditable(false);status1.setEnabled(false);JLabel l=new JLabel(状态: );R0.add(l);R0.add(status0);R1.add(status2);R1.add(status3);R2.add(status4);R2.add(status5);b=new JButton(发送);b.setEnabled(false);R3.add(b);JLabel l1=new JLabel(选择操作 );Q1.add(l1);/ 选择操作JRadioButton r1=new JRadioButton(ACK),new JR
11、adioButton(NAK),new JRadioButton(不处理),new JRadioButton(初始 );r=r1;ButtonGroup rg=new ButtonGroup();for(int i=0;ir.length-1;i+)Q1.add(ri);rg.add(ri); rg.add(r3); /数据收发住状态列表m_data=new DefaultTableModel();m_view=new JTable(m_data);m_view.setPreferredScrollableViewportSize(new Dimension(300,230); /表格大小 m
12、_view.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);JScrollPane sPane=new JScrollPane(m_view);P1.add(sPane);T=new Time(status3);/发送操作b.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) T.time_start();newtore();status0.setText(新数据);r3.setSelected(true););/ 时间显示 监听Document d
13、t = status3.getDocument();dt.addDocumentListener(new DocumentListener()public void changedUpdate(DocumentEvent e) ;public void insertUpdate(DocumentEvent e) if(status3.getText().toString().equals(00:00:00)T.time_restart();newtore();String s=status5.getText().toString();mb_addRow(s, 超时, 重发, ); public
14、 void removeUpdate(DocumentEvent e) ;);/输入监听 Document dt1 = status5.getDocument();dt1.addDocumentListener(new DocumentListener()public void changedUpdate(DocumentEvent e) public void insertUpdate(DocumentEvent e) if(!status5.getText().toString().equals() b.setEnabled(true); ;public void removeUpdate(DocumentEvent e) if(status5.getText().toString().equals()b.setEnabled(false););/按钮监听r0.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e) T.time_stop();retonew();status5