java 基于Socket局域网文件传输v1.3

为什么我会做这个软件呢

因为我们宿舍喜欢打打求生之路 安装第三方地图的时候一般都是一人下载 然后通过QQ发给其他人

QQ传输速度太慢,于是开发了这个软件

时间2018.1.11

发送.java

package v1_3;

import java.awt.Component;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JDialog;

import java.awt.Font;
import java.awt.Graphics;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.awt.event.ActionEvent;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.Color;

public class 发送 extends JFrame {

	private JPanel contentPane;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					发送 frame = new 发送(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public static 发送 发送;
	public static String mulu;
	public 发送(Component toto) {
		setResizable(false);
		setTitle("\u5C40\u57DF\u7F51\u6587\u4EF6\u4F20\u8F93-\u53D1\u9001\u65B9V1.2");
		发送=this;
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				super.windowClosing(arg0);
				if(fasong.list.size()>0){
//					javax.swing.JOptionPane.showMessageDialog(发送.this, "正在传输文件请稍后再试");
					try {
						Message dialog = new Message(发送.this, "正在传输文件请稍后再试...",278,129);
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);
					} catch (Exception ee) {
						ee.printStackTrace();
					}
				}
				else{
					System.exit(0);
				}
			}
		});
		setDefaultCloseOperation(0);
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(toto);
		setLocation(this.getX()-this.getWidth()/2, this.getY());
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBackground(Color.WHITE);
		textField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		textField.setEditable(false);
		textField.setBounds(10, 194, 414, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label = new JLabel("\u6587\u4EF6\u8DEF\u5F84\uFF1A(\u628A\u6587\u4EF6\u62D6\u5230\u8FD9\u91CC)");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		label.setBounds(10, 169, 213, 15);
		contentPane.add(label);
		
//		JButton btnNewButton = new JButton("\u6D4F\u89C8");
//		btnNewButton.setBackground(Color.WHITE);
//		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
//		btnNewButton.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent e) {
//				JFileChooser jf = new JFileChooser();
//				if (jf.showOpenDialog(null) == 0) {
//					mulu = jf.getSelectedFile().getPath();
//					textField.setText(mulu);
//				}
//			}
//		});
//		btnNewButton.setBounds(350, 193, 74, 23);
//		contentPane.add(btnNewButton);
		
		JButton button = new JButton("\u53D1\u9001");
		button.setBackground(Color.WHITE);
		button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(textField.getText().equals("")){
//					javax.swing.JOptionPane.showMessageDialog(发送.this, "请设置文件路径...");
					try {
						Message dialog = new Message(发送.this, "请设置文件路径...",278,129);
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);
					} catch (Exception ee) {
						ee.printStackTrace();
					}
				}
				else if(fasong.list.size()>0){
//					javax.swing.JOptionPane.showMessageDialog(发送.this, "正在传输文件请稍后再试");
					try {
						Message dialog = new Message(发送.this, "正在传输文件请稍后再试",278,129);
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);
					} catch (Exception ee) {
						ee.printStackTrace();
					}
				}
				else{//////////////
					File fi=new File(textField.getText());
					if(!fi.isFile()){
//						javax.swing.JOptionPane.showMessageDialog(发送.this, "非法文件 请重新选择");
						try {
							Message dialog = new Message(发送.this, "非法文件 请重新选择",278,129);
							dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
							dialog.setVisible(true);
						} catch (Exception ee) {
							ee.printStackTrace();
						}
						return;
					}
					///////////////
					Socket so=null;
					for(int i=0;i<fwq.list.size();i++){
						so=fwq.list.get(i);
						new Thread(new fasong(so)).start();
					}
					new Thread(new drawaaa()).start();
					
				}
			}
		});
		button.setBounds(331, 228, 93, 23);
		contentPane.add(button);
		
		JLabel lblNewLabel = new JLabel("\u8BF7\u8BA9\u5BF9\u65B9\u8FDE\u63A5\u5230\u60A8\u7684IP\uFF1A");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		lblNewLabel.setBounds(10, 128, 414, 31);
		contentPane.add(lblNewLabel);
		////////////////////////////////////////////////////////////////////////
		String s=null;
		try {
			s = "" + InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		StringBuffer ss = new StringBuffer();
		try {
			for (int i = s.indexOf("192"); i < s.length(); i++) {
				ss.append(s.charAt(i));
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		lblNewLabel.setText("\u8BF7\u8BA9\u5BF9\u65B9\u8FDE\u63A5\u5230\u60A8\u7684IP\uFF1A" + ss);
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		
		label_1.setBounds(289, 0, 145, 184);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("\u5982\u679C\u65E0\u6CD5\u8FDE\u63A5\u8BF7\u5C06\u672C\u673A\u9632\u706B\u5899\u5173\u95ED");
		label_2.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		label_2.setBounds(10, 233, 236, 15);
		contentPane.add(label_2);
		new Thread(new fwq()).start();
		//////////////////////////////////////////////////////////////////////////////////////

		new DropTarget(this,new DropTargetAdapter() {
			
			public void drop(DropTargetDropEvent arg0) {
				发送.this.drop(arg0);
//	            arg0.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);//接受滴 没这句就会报错
//	            arg0.acceptDrop(3);//接受滴 没这句就会报错 3表示移动or复制
//				List<?> list=null;
//				try {
//					list = (List<?>) arg0.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
//										//get可转让的		get数据传输		数据的味道。java文件列表的味道
//					File fi=(File)list.get(0);
//					mulu=fi.getPath();
//					textField.setText(mulu);
//				} catch (UnsupportedFlavorException | IOException e) {
//					e.printStackTrace();
//				}
			}

		});
		new DropTarget(textField,new DropTargetAdapter() {
			
			public void drop(DropTargetDropEvent arg0) {
				发送.this.drop(arg0);
			}
		});
	}
	public static JLabel label_1 = new JLabel("");

	public void paint(Graphics g) {
		super.paint(g);
//		int daxiao=12;
//		int weizhi=4;
//		int x=11;
//		g.setFont(new Font("微软雅黑",0,daxiao));
//		g.drawString("已连接:"+fwq.list.size(), x, daxiao*weizhi++);
		g.drawString("已连接:"+fwq.list.size(),11,48);
	}
	public void drop(DropTargetDropEvent arg0){
		arg0.acceptDrop(3);//接受滴 没这句就会报错 3表示移动or复制
		List<?> list=null;
		try {
			list = (List<?>) arg0.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
								//get可转让的		get数据传输		数据的味道。java文件列表的味道
			File fi=(File)list.get(0);
			mulu=fi.getPath();
			textField.setText(mulu);
		} catch (UnsupportedFlavorException | IOException e) {
			e.printStackTrace();
		}
	}
}
class fwq implements Runnable{
	public static ArrayList<Socket> list;
	public static int yilianjie=0;

	public void run() {
		list=new ArrayList<Socket>();
		ServerSocket soo=null;
		Executor ex=Executors.newCachedThreadPool();
		try {
			soo=new ServerSocket(61855);
		} catch (IOException e) {
//			e.printStackTrace();
//			javax.swing.JOptionPane.showMessageDialog(发送.发送, "端口61855已打开 启动程序失败 可能程序已经启动...");
			try {
				Message dialog = new Message(发送.发送, "<html>端口61855已打开 启动程序失败 可能程序已经启动..."
						+ "<br>程序将在5秒内退出",380,129);
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			} catch (Exception ee) {
				ee.printStackTrace();
			}
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
//			System.exit(1);
		}
		while(true){
		try {
			Socket so=soo.accept();//等待连接s
			list.add(so);
			yilianjie++;
			发送.发送.repaint();
			ex.execute(new dklj(so));
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
		
		
		
	}
	class dklj implements Runnable{
		Socket so=null;
		dklj(Socket so){
			this.so=so;
		}
		public void run() {
			BufferedReader in=null;
			while(true){
				try {
					in=new BufferedReader(new InputStreamReader(so.getInputStream()));
					if(in.readLine()==null){break;}
				} catch (IOException e) {
//					e.printStackTrace();
//					System.out.println("一位用户已退出...");
					break;
				}
				
			}
			fwq.yilianjie--;
			list.remove(so);
			发送.发送.repaint();
			try {
				if(in!=null)in.close();
				if(so!=null)so.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
	
}
class fasong implements Runnable{
	public static ArrayList<aaa> list=new ArrayList<aaa>();
	Socket so=null;
	private aaa aaa;
	public fasong(Socket so){
		this.so=so;
		aaa=new aaa();
		list.add(aaa);
	}

	public void run() {
		File fi=new File(发送.mulu);
		BufferedInputStream in=null;
		try {
			in=new BufferedInputStream(new FileInputStream(fi));
			DataOutputStream out=new DataOutputStream(new BufferedOutputStream(so.getOutputStream()));
			String name=fi.getName();
			long size=fi.length();long yifasong=0;
			int len;
			byte[] by=new byte[1024*1024*10];
			out.writeUTF(name);
			out.flush();
			out.writeLong(size);
			out.flush();
			
			while((len=in.read(by))!=-1){
				out.write(by,0,len);
				out.flush();
				yifasong+=len;
//				System.out.println("已发送:"+yifasong*100.0/size+"%");
				double bfb=yifasong*100.0/size;
				bfb=(int)(bfb*10);
				bfb/=10;
				aaa.s="已发送:"+bfb+"%";
//				发送.发送.repaint();
			}
			list.remove(aaa);
//			发送.发送.repaint();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if(in!=null)in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		发送.发送.repaint();
		
		
	}
	
}
class aaa{
	public String s="已发送:0.0%";
}
class drawaaa implements Runnable{

	public void run() {
		while(fasong.list.size()>0){
			try {
				Thread.sleep(0);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			aaa aaa=null;
			StringBuffer str=new StringBuffer("<html>");
			for(int i=0;i<fasong.list.size();i++){
				aaa=fasong.list.get(i);
				str.append(aaa.s);
				str.append("<br>");
			}
			发送.label_1.setText(str.toString());
		}
		发送.label_1.setText("已完成");
		发送.发送.repaint();
//		javax.swing.JOptionPane.showMessageDialog(发送.发送, "传输完成!");
		try {
			Message dialog = new Message(发送.发送, "传输完成!",278,129);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception ee) {
			ee.printStackTrace();
		}
	}
	
}

接受.java

package v1_3;

import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileSystemView;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.Socket;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Color;

public class 接受 extends JFrame {

	private JPanel contentPane;
	private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					接受 frame = new 接受(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	private void lujingout(){
		BufferedWriter out=null;
		try {
			out=new BufferedWriter(new FileWriter("Set.txt"));
			out.write(textField_1.getText()+"\r\n");
		} catch (IOException e) {
//			e.printStackTrace();
		}
		try {
			if (out!=null) {
				out.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	private void lujingin(){
		BufferedReader in=null;
		try {
			in=new BufferedReader(new FileReader("Set.txt"));
			String s=in.readLine();
			textField_1.setText(s);
		} catch (IOException e) {
//			e.printStackTrace();
		}
		try {
			if(in!=null){
				in.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static boolean yilianjie=false;
	public static boolean jinzhiguanbi=false;
	public 接受(Component toto) {
		setResizable(false);
		setTitle("\u5C40\u57DF\u7F51\u6587\u4EF6\u4F20\u8F93-\u63A5\u6536\u65B9V1.2");
		FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了,这里重要的一句
//		System.out.println(fsv.getHomeDirectory());                //得到桌面路径
		setDefaultCloseOperation(0);
		this.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent arg0) {
			super.windowClosing(arg0);
			if(jinzhiguanbi){
				javax.swing.JOptionPane.showMessageDialog(接受.this, "正在传输文件...");
			}
			else{
				lujingout();
				System.exit(0);
			}
		}
		});
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(toto);
		setLocation(this.getX()+this.getWidth()/2, this.getY());
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		textField.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!yilianjie) {
					new Thread(new lianjie(textField.getText())).start();
				} else {
//					javax.swing.JOptionPane.showMessageDialog(接受.this, "您已连接服务器 请不要重复点击...");
					try {
						Message dialog = new Message(接受.this, "已连接服务器...",278,129);
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);
					} catch (Exception ee) {
						ee.printStackTrace();
					}
				}
			}
		});
		textField.setText("192.168.1.");
		textField.setBounds(46, 139, 260, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton button = new JButton("\u8FDE\u63A5");
		button.setBackground(Color.WHITE);
		button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!yilianjie){
				new Thread(new lianjie(textField.getText())).start();
				}else{
//					javax.swing.JOptionPane.showMessageDialog(接受.this, "您已连接服务器 请不要重复点击...");
					try {
						Message dialog = new Message(接受.this, "已连接服务器...",278,129);
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);
					} catch (Exception ee) {
						ee.printStackTrace();
					}
				}
			}
		});
		button.setBounds(316, 138, 93, 23);
		contentPane.add(button);
		lblip.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		
		lblip.setBounds(10, 114, 414, 15);
		contentPane.add(lblip);
		
		textField_1 = new JTextField(fsv.getHomeDirectory().toString()+"\\");
		textField_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lujingin();
		textField_1.setBounds(46, 195, 260, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		label = new JLabel("\u53E6\u5B58\u4E3A\u76EE\u5F55\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		label.setBounds(10, 170, 109, 15);
		contentPane.add(label);
		
		button_1 = new JButton("\u6D4F\u89C8");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				javax.swing.JOptionPane.showMessageDialog(接受.this, "此按钮不可用,请将保存路径复制到编辑框");
				try {
					Message dialog = new Message(接受.this, "此按钮不可用 请将保存路径复制到编辑框",278,129);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception ee) {
					ee.printStackTrace();
				}
			}
		});
		button_1.setBackground(Color.WHITE);
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button_1.setBounds(316, 194, 93, 23);
		contentPane.add(button_1);
		
		lblNewLabel = new JLabel("");
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		lblNewLabel.setBounds(142, 60, 144, 15);
		contentPane.add(lblNewLabel);
		
		button_2 = new JButton("\u8BBE\u4E3A\u684C\u9762");
		button_2.setBackground(Color.WHITE);
		button_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				FileSystemView fsv = FileSystemView.getFileSystemView();  //注意了,这里重要的一句
				textField_1.setText(fsv.getHomeDirectory()+"\\");
			}
		});
		button_2.setBounds(213, 220, 93, 23);
		contentPane.add(button_2);
		
		button_3 = new JButton("\u4F7F\u7528\u4E0A\u4E00\u6B21\u7684\u8DEF\u5F84");
		button_3.setBackground(Color.WHITE);
		button_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				lujingin();
			}
		});
		button_3.setBounds(56, 220, 147, 23);
		contentPane.add(button_3);
		接受=this;
	}
	public static 接受 接受;
	public static JLabel lblip = new JLabel("\u8BF7\u5148\u8FDE\u63A5\u5230\u5BF9\u65B9IP\uFF1A");
	public static JTextField textField_1;
	private JLabel label;
	private JButton button_1;
	public static JLabel lblNewLabel;
	private JButton button_2;
	private JButton button_3;

}
class lianjie implements Runnable{
	public Socket so;
	private String ip;
	public lianjie(String s){
		this.ip=s;
	}

	public void run() {
		try {
			接受.lblip.setText("正在连接"+ip+"...");
			so=new Socket(ip,61855);
			接受.lblip.setText("已连接"+ip+" 等待对方发送文件...");
			接受.yilianjie=true;
			new Thread(new shouwenjian()).start();
		} catch (IOException e) {
//			e.printStackTrace();
			if(接受.yilianjie)
				接受.lblip.setText("连接失败请检查IP是否正确");
		}
	}
	class shouwenjian implements Runnable{

		public void run() {
			BufferedOutputStream out=null;
			DataInputStream in=null;
			try {
				while (true) {
					in = new DataInputStream(new BufferedInputStream(so.getInputStream()));
					byte[] by = new byte[1024 * 1024 * 10];
					String name = in.readUTF();
					long size = in.readLong();
					接受.jinzhiguanbi=true;
					long jieshou = 0;
					int len;
					String s=接受.textField_1.getText();
					char c=s.charAt(s.length()-1);
					if(c!='\\'){
						s+="\\";
					}
					//
					File mulu=new File(s);
					if (!mulu.getParentFile().exists()) {
						mulu.getParentFile().mkdirs();
					}
					// 判断文件是否存在,不存在就创建文件
					if (!mulu.exists() && !mulu.isDirectory()) {
						mulu.mkdir();
					}
					//
					File fi = new File(s + name);
					out = new BufferedOutputStream(new FileOutputStream(fi));//可能会出错
					while ((len = in.read(by)) != -1) {
						out.write(by, 0, len);
						out.flush();
						jieshou += len;
						double bfb=jieshou*100.0/size;
						bfb=(int)(bfb*10);
						bfb/=10;
//						接受.lblNewLabel.setText("已接收:" + jieshou*100.0 / size  + "%");
						接受.lblNewLabel.setText("已接收:" + bfb  + "%");
//						接受.接受.repaint();
						if (jieshou >= size) {
							break;
						}
					}
					out.close();
//					javax.swing.JOptionPane.showMessageDialog(接受.接受, "文件接收完成...");
					try {
						Message dialog = new Message(接受.接受, "文件接收完成...",278,129);
						dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
						dialog.setVisible(true);
					} catch (Exception ee) {
						ee.printStackTrace();
					}
					接受.jinzhiguanbi=false;
				}
			} catch (IOException e) {
				e.printStackTrace();
//				javax.swing.JOptionPane.showMessageDialog(接受.接受, "文件路径异常或发送方已关闭软件");
				try {
					Message dialog = new Message(接受.接受, "文件路径异常或发送方已关闭软件",278,129);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception ee) {
					ee.printStackTrace();
				}
			}
			
			///////////////////
			接受.jinzhiguanbi=false;
			接受.yilianjie=false;
			接受.lblip.setText("已断开连接");
			try {
				if(out!=null)out.close();
				if(in!=null)in.close();
				if(so!=null)so.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
		}
		
	}
	
}

入口.java

package v1_3;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JDialog;

import java.awt.Color;
import java.awt.Font;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.awt.event.ActionEvent;

public class 入口 extends JFrame {

	private JPanel contentPane;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					入口 frame = new 入口();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
				System.out.println("请不要关闭此窗口!!!");
				System.out.println("请不要关闭此窗口!!!");
				System.out.println("请不要关闭此窗口!!!");
				System.out.print("重要是事情要说三遍");
			}
		});
	}

	public 入口() {
		setResizable(false);
		setTitle("\u5C40\u57DF\u7F51\u4F20\u8F93\u6587\u4EF6v1.2");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(450, 300);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JButton btnNewButton = new JButton("\u53D1\u6587\u4EF6");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				发送 f=new 发送(入口.this);
				入口.this.setVisible(false);
				f.setVisible(true);
			}
		});
		btnNewButton.setBackground(Color.WHITE);
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		btnNewButton.setBounds(10, 81, 414, 32);
		contentPane.add(btnNewButton);
		
		JButton button = new JButton("\u6536\u6587\u4EF6");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				接受 f=new 接受(入口.this);
				入口.this.setVisible(false);
				f.setVisible(true);
			}
		});
		button.setBackground(Color.WHITE);
		button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		button.setBounds(10, 123, 414, 32);
		contentPane.add(button);
		
		JButton button_1 = new JButton("\u5173\u4E8E");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				javax.swing.JOptionPane.showMessageDialog(入口.this,"BY:李子果");
				try {
					Message dialog = new Message(入口.this,"<html>BY:李子果",278,129);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception ee) {
					ee.printStackTrace();
				}
			}
		});
		button_1.setBackground(Color.WHITE);
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		button_1.setBounds(331, 228, 93, 23);
		contentPane.add(button_1);
		
		JButton btnNewButton_1 = new JButton("\u7248\u672C\u8BF4\u660E");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				javax.swing.JOptionPane.showMessageDialog(入口.this, "版本v1.2-新增拖拽文件上传 接发文件合并-兼容v1.1\n"
//						+ "版本v1.1-修复大部分bug 新外观 支持多次传输\n"
//						+ "版本v1.0-无介绍");
//				new Message(入口.this,"<html>版本v1.2-新增拖拽文件上传 接发文件合并-兼容v1.1<br>"
//						+ "版本v1.1-修复大部分bug 新外观 支持多次传输<br>"
//						+ "版本v1.0-无介绍",374,158);
				
				try {
					Message dialog = new Message(入口.this,"<html>版本v1.3-优化外观-兼容v1.2 v1.1<br>"
							+ "版本v1.2-新增拖拽文件上传 接发文件合并-兼容v1.1<br>"
							+ "版本v1.1-修复大部分bug 新外观 支持多次传输<br>"
							+ "版本v1.0-无介绍",374,158);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				} catch (Exception ee) {
					ee.printStackTrace();
				}
			}
		});
		btnNewButton_1.setBackground(Color.WHITE);
		btnNewButton_1.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		btnNewButton_1.setBounds(10, 228, 93, 23);
		contentPane.add(btnNewButton_1);
//		new DropTarget(this,new DropTargetAdapter() {
//			
//			public void drop(DropTargetDropEvent arg0) {
//				arg0.acceptDrop(3);
//				try {
//					List<?> list=(List<?>) arg0.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
//					File fi=(File)list.get(0);
//					System.out.println(fi.getPath());
//				} catch (UnsupportedFlavorException | IOException e) {
//					// TODO 自动生成的 catch 块
//					e.printStackTrace();
//				}
//			}
//		});
	}

}

Message.java

package v1_3;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Component;

import javax.swing.SwingConstants;

public class Message extends JDialog {

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Message dialog = new Message(null,"<html>123<br>123",450,300);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			System.out.println(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Message(Component a,String s,int w,int h) {
		setTitle("\u6D88\u606F");
		setResizable(false);
		getContentPane().setBackground(Color.WHITE);
//		setBounds(100, 100, 450, 300);
		setSize(w, h);
		setLocationRelativeTo(a);
		getContentPane().setLayout(null);
		
		JLabel lblNewLabel = new JLabel(s);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 12));
//		lblNewLabel.setBounds(10, 10, 414, 208);
		lblNewLabel.setBounds(10, 10, this.getWidth()-36, this.getHeight()-92);
		getContentPane().add(lblNewLabel);
		
		JButton btnNewButton = new JButton("\u786E\u5B9A");
		btnNewButton.setBackground(Color.WHITE);
		btnNewButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Message.this.dispose();
			}
		});
		btnNewButton.setBounds(331, 228, 93, 23);
		getContentPane().add(btnNewButton);
		btnNewButton.setLocation(this.getWidth()-btnNewButton.getWidth()-26, this.getHeight()-btnNewButton.getHeight()-49);
	}
}

下载地址:https://download.csdn.net/download/u010756046/10482469


猜你喜欢

转载自blog.csdn.net/u010756046/article/details/80713731