主要思路很简单:
1.设置自己的接收端口
2.设置对方IP和端口
3.发送数据和接收数据
下面是主要代码:
import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; public class QQFrame extends JFrame implements Runnable { private JTextArea sendArea = new JTextArea(20,20); private JTextArea showArea = new JTextArea(10,20); private JButton sendButton = new JButton("发送"); private MenuBar menuBar = new MenuBar(); private Menu setMenu = new Menu("设置"); private Menu helpMenu = new Menu("帮助"); private MenuItem aboutItem = new MenuItem("关于"); private MenuItem setItem = new MenuItem("配置参数"); private Thread thread = new Thread(this); private Config config = new Config(); private JDialog dialog = new JDialog(this,"参数配置",true); private int myPort; QQFrame(int in_myPort) { super("局域网聊天工具"); setResizable(false); myPort = in_myPort; setItem.addActionListener(new SetDialog(dialog,this)); aboutItem.addActionListener(new AboutDialog(this)); initLayout(); sendButton.addActionListener(new SendListener(this,sendArea,showArea,config)); thread.start(); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); } void initLayout() { int w,h; w = 400; h = 500; setBounds(100,100,w,h); setVisible(true); setLayout(new GridLayout(3,1,10,15)); JPanel jp1,jp2; jp1 = new JPanel(); jp2 = new JPanel(); JLabel jl1; jl1 = new JLabel("聊天记录框:"); jp1.setLayout(null); jp1.add(jl1); jl1.setBounds(0,0,w,20); jp1.add(showArea); showArea.setBounds(0,30,w,h/3-20); JLabel jl2; jp2.setLayout(null); jl2 = new JLabel("发送框:"); jp2.add(jl2); jl2.setBounds(0,0,w,20); sendArea.setBounds(0,30,w,h/3-20); jp2.add(sendArea); JPanel jp3 = new JPanel(); add(jp1); add(jp2); add(jp3); jp3.add(sendButton); setMenuBar(menuBar); menuBar.add(setMenu); menuBar.add(helpMenu); helpMenu.add(aboutItem); setMenu.add(setItem); dialog.setBounds(120,150,200,250); JLabel IPLabel = new JLabel("对方IP"); JLabel yourPortLabel = new JLabel("对方接收端口"); JLabel nameLabel = new JLabel("本机名"); JTextField IPTextField = new JTextField(15); JTextField yourPortTextField = new JTextField(8); JTextField nameTextField = new JTextField(15); IPTextField.setText("127.0.0.1"); yourPortTextField.setText("879"); nameTextField.setText("hanxi"); dialog.setLayout(new GridLayout(4,2,10,10)); dialog.add(IPLabel); dialog.add(IPTextField); dialog.add(yourPortLabel); dialog.add(yourPortTextField); dialog.add(nameLabel); dialog.add(nameTextField); dialog.add(new Panel()); JButton sureButton = new JButton("确定"); dialog.add(sureButton); sureButton.addActionListener(new SureListener(IPTextField,yourPortTextField,nameTextField,dialog,config)); } public void run() { DatagramPacket dp = null; DatagramSocket ds = null; byte[] buf = new byte[1024]; try { dp = new DatagramPacket(buf,buf.length); ds = new DatagramSocket(myPort); System.out.println("myport="+myPort); } catch (Exception e) {} while (true) { try { ds.receive(dp); int length = dp.getLength(); InetAddress address = dp.getAddress(); int port = dp.getPort(); String message = new String(dp.getData(),0,length); showArea.append("收到数据长度:"+length+"\n"); showArea.append("收到数据来自:"+address+"端口:"+port+"\n"); showArea.append("收到数据是:"+message+"\n"); } catch (Exception e) { } } } } //发送事件 class SendListener implements ActionListener { private JFrame QQFrame; private JTextArea sendArea; private JTextArea showArea; private Config config; SendListener(JFrame in_QQFrame, JTextArea in_sendArea, JTextArea in_showArea, Config in_config) { QQFrame = in_QQFrame; sendArea = in_sendArea; showArea =in_showArea; config = in_config; System.out.println("in_port:"+config.getyourPort()); QQFrame.validate(); } public void actionPerformed(ActionEvent e) { System.out.println("你点击了发送按钮"); byte[] buf = sendArea.getText().trim().getBytes(); try { InetAddress address = InetAddress.getByName(config.getIP()); DatagramPacket dp = new DatagramPacket(buf,buf.length,address,Integer.parseInt(config.getyourPort())); DatagramSocket ds = new DatagramSocket(); showArea.append("数据包目标地址:"+dp.getAddress()+"\n"); showArea.append("数据包目标端口号:"+dp.getPort()+"\n"); showArea.append("数据包长度:"+dp.getLength()+"\n"); ds.send(dp); } catch (Exception ee) {} } } //配置设置 class SetDialog implements ActionListener { private JDialog dialog; private JFrame frame; SetDialog(JDialog in_dialog,JFrame parent) { dialog = in_dialog; frame = parent; } public void actionPerformed(ActionEvent e) { dialog.validate(); int x = frame.getX(); int y = frame.getY(); dialog.setBounds(x+50,y+50,200,250); dialog.setVisible(true); } } //关于对话框 class AboutDialog extends JDialog implements ActionListener { private JFrame frame; private JLabel jl = new JLabel("感谢您的使用,作者:涵曦"); AboutDialog(JFrame parent) { frame = parent; add(jl); } public void actionPerformed(ActionEvent e) { validate(); int x = frame.getX(); int y = frame.getY(); setBounds(x+50,y+50,200,250); setVisible(true); } } //对话框的确定按钮事件 class SureListener implements ActionListener { JTextField IPTextField; JTextField yourPortTextField; JTextField nameTextField; private Dialog dialog; private Config config; SureListener(JTextField in_IPTextField,JTextField in_yourPortTextField,JTextField in_nameTextField,JDialog in_dialog,Config in_config) { IPTextField = in_IPTextField; yourPortTextField = in_yourPortTextField; nameTextField = in_nameTextField; dialog = in_dialog; config = in_config; } public void actionPerformed(ActionEvent e) { config.set(IPTextField.getText(),yourPortTextField.getText(),nameTextField.getText()); System.out.println("config:yourPort:"+config.getyourPort()); dialog.setVisible(false); } } //参数 class Config { private String IP="127.0.0.1"; private String yourPort="666"; private String name="hanxi"; public void set(String in_IP, String in_yourPort,String in_name) { IP = in_IP; yourPort = in_yourPort; name = in_name; } public String getIP() { return IP; } public String getyourPort() { return yourPort; } public String getName() { return name; } }
下面是主方法代码:
public class QQ { public static void main(String args[]) { if (args.length == 0) { System.out.println("请在QQ后面输入接收数据的端口号(需要空格)!"); System.exit(0); } System.out.println(Integer.parseInt(args[0])); QQFrame frame = new QQFrame(Integer.parseInt(args[0])); } }
设置端口和IP的图片
下面是聊天界面
时间: 2024-09-30 06:33:51