qq_36307232
qq_36307232
2017-05-07 06:04

java点击按钮事件,弹出窗口出错 求帮助啊0.0

20
  • java
  • 文件传输
  • socket

部分代码如下:

//触发事件
class rfListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new ReceiveFile().start();
}
}
//窗口
public class ReceiveFile extends JFrame implements ActionListener{
private static final long serialVersionUID = 1L;
private JButton save = new JButton("保存文件");
private DatagramPacket dp;
private DatagramSocket ds;
private FileOutputStream fos;
private FileInputStream fis;
private String filename;
private byte[] buf = new byte[10240];

public ReceiveFile(){

     this.setSize(300,200);
     this.setLocation(500, 300);
     this.setTitle("接收文件");
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.setLayout(new FlowLayout());
     this.add(save);
     save.addActionListener(this);
     this.setVisible(true);

}

public void start(){
    try {
        ds = new DatagramSocket(4567);
        fos = new FileOutputStream("d:\\tem.dat");
            while(true){
                    dp = new DatagramPacket(buf, buf.length);
                    ds.receive(dp);
                    fos.write(dp.getData());
                    fos.flush();
            }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}

弹出窗口后应该是这样的:图片说明
但是结果是这样的:图片说明

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐