收录日期:2018/06/20 09:34:08 时间:2016/05/18 03:14:08 标签:Java SE
我做的程序有些问题,首先发送两次就产生异常,接收不论怎样都是异常,是不是里面算法有问题?还是定时器函数调用不正确,高手们麻烦帮我改下,小弟不胜感激,分不够可以再加,里面mistake暂时没用,代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.util.*;

public class Frame1 extends JFrame{
  private JPanel contentPane;
  private XYLayout xYLayout1 = new XYLayout();
  private TextField textField1 = new TextField();
  private TextField textField2 = new TextField();
  private TextField textField3 = new TextField();
  private Button button1 = new Button();
  private Button button2 = new Button();
  private Button button3 = new Button();
  private Label label1 = new Label();
  private Label label2 = new Label();
  private Label label3 = new Label();
  int send_flag=0;
  int receive_flag=0;
  java.util.Timer sendcol=new  java.util.Timer();
  java.util.Timer receivecol=new  java.util.Timer();
  java.util.Timer colflag=null;
  String ssend;
  String sreceive;
  String strin;
  String sbuffer="";
  boolean chose,chose1,answer;
  //boolean chosereceive=true;
  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    textField1.setFont(new java.awt.Font("Dialog", 0, 14));
    textField1.setText("01234567");
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("滑动窗口");
    textField2.setFont(new java.awt.Font("Dialog", 0, 14));
    textField2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
       // textField2_actionPerformed(e);
      }
    });
    textField3.setFont(new java.awt.Font("Dialog", 0, 14));
    textField3.setText("76543210");
    button1.setLabel("开始发送");
    button1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button1_actionPerformed(e);
      }
    });
    button2.setLabel("mistake");
    button3.setLabel("开始接收");
    button3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button3_actionPerformed(e);
      }
    });
    label1.setText("发送端口");
    label2.setText("缓冲区");
    label3.setText("接收窗口");
    contentPane.setFont(new java.awt.Font("Dialog", 0, 14));
    contentPane.add(button1,   new XYConstraints(266, 54, 92, 22));
    contentPane.add(textField3, new XYConstraints(66, 225, 121, 27));
    contentPane.add(textField1, new XYConstraints(69, 60, 116, 26));
    contentPane.add(label1,  new XYConstraints(70, 33, 62, 16));
    contentPane.add(label2,  new XYConstraints(61, 116, 73, 19));
    contentPane.add(label3,  new XYConstraints(63, 193, 64, 20));
    contentPane.add(button2,  new XYConstraints(265, 104, 93, 24));
    contentPane.add(button3,  new XYConstraints(267, 165, 89, 24));
    contentPane.add(textField2, new XYConstraints(65, 144, 120, -1));
  }
  //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);
    }
  }

  class slide extends TimerTask{
    public void run(){
      int i=0,j=0;
      java.util.Random rand=new Random();
      if(chose){//定时器sendcol,相当于按钮开始发送有效,开始发送
      ssend=textField1.getText();
      sbuffer+=ssend.valueOf(ssend.charAt(0));
      strin+=ssend.valueOf(ssend.charAt(0));//存储未接收到应答的帧的序号
      textField2.setText(sbuffer); //发送端输入到缓冲区
      textField1.setText(ssend.substring(1));//删除发送了的帧序号
      }

      if(chose1){//定时器receivecol,相当于按钮开始接收有效,开始接收
        if(textField2.getText()!=null){//非空时,从缓冲区删除,同时也删除接收区的帧号,
                                            //表示接收区已接收到帧
         sreceive=textField3.getText();
         textField3.setText(sreceive.substring(0,sreceive.length()-2));
         //删除接收区的帧号
         textField2.setText(ssend.substring(1));
         //删除缓冲区的帧号
        }

        if(answer){//有应答信号所有的处理
          i=strin.length()-sbuffer.length();//除去缓冲区以外未应答的帧数
          while(j<i){//把缓冲区外未应答的帧号插回到发送区和接收区
          ssend+=strin.valueOf(strin.charAt(0));//插到发送区
          StringBuffer sb=new StringBuffer(sreceive);//插到接收区
          sb=sb.insert(0,strin.valueOf(strin.charAt(0)));
          sreceive=sb.toString();
          textField3.setText(sreceive);
          strin=strin.substring(1); //删除已插到发送区和接收区的帧号
          j++; //控制循环
          }
          j=0;
        }
        answer=false;
        if(rand.nextInt()%2==0){
          i=0;
          answer=true;
        }
      }

    }
  }
  slide slide1=new slide();

  void button1_actionPerformed(ActionEvent e)
  {
    chose=true;
    if(send_flag==0){//按钮转换,0时启动定时器,为正在发送。1时关闭定时器,为停止发送
      sendcol.schedule(slide1,1000,1000); //启动定时器
      //}
      send_flag++;
      button1.setLabel("停止发送");//按钮的显示转换
    }
    else{
      sendcol.cancel(); //关闭定时器
      send_flag--;

      button1.setLabel("开始发送");
    }
  }

  void button3_actionPerformed(ActionEvent e) {
     chose1=true;
    if(receive_flag==0){//按钮转换,0时启动定时器,为正在接收。1时关闭定时器,为停止接收

      receivecol.schedule(slide1,1000,1000);

      receive_flag++;
      button3.setLabel("停止接收");
    }
    else{

      receivecol.cancel();
      receive_flag--;
      button3.setLabel("开始接收");
    }
  }

}



















up,做个标记
各位大哥,帮个忙啊!!!
很想帮你解决,只可惜我在网吧,没有办法帮你调试了,关注!!!
各位大哥,帮个忙啊!!!
加分讲一声,小弟这有!!
可惜我不用jbuilder。
我有办法,回去告诉你!!