java swing 已经基本上忘的差不多了,没事的时候开始练手了。希望走过路过的大虾不吝赐教,多多指点~~~谢先..
下面做的是一个一对一聊天的小程序:
第一步:设计视图;
package j2se.gui;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.List;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MainFrame extends Frame {
private List list;
private Panel panel;
private Panel ipPanel;
private Panel dataPanel;
private TextField txtIP;
private TextField txtData;
private WindowClosingDispose windowClosingDispose;
private Label labelIP;
private Label labelData;
public MainFrame() {
setData();
setShow();
setFrame();
setListener();
}
private void setData() {
list = new List(10);
panel = new Panel();
ipPanel = new Panel();
dataPanel = new Panel();
labelIP = new Label("对方IP:");
labelData = new Label("您要说:");
txtIP = new TextField(20);
txtIP.setText("127.0.0.1");
txtData = new TextField(50);
}
private void setShow() {
this.add(list,BorderLayout.CENTER);
this.add(panel,BorderLayout.SOUTH);
panel.setLayout(new BorderLayout());
ipPanel.setLayout(new BorderLayout());
dataPanel.setLayout(new BorderLayout());
ipPanel.add(labelIP,BorderLayout.WEST);
ipPanel.add(txtIP,BorderLayout.EAST);
dataPanel.add(labelData,BorderLayout.WEST);
dataPanel.add(txtData,BorderLayout.EAST);
panel.add(ipPanel,BorderLayout.WEST);
panel.add(dataPanel,BorderLayout.EAST);
windowClosingDispose = this.new WindowClosingDispose();
}
private void setFrame() {
this.setBounds(200, 100, 700, 500);
this.setTitle("Chat");
this.setVisible(true);
}
private void setListener() {
this.addWindowListener(windowClosingDispose);
}
private class WindowClosingDispose extends WindowAdapter{
@Override
public void windowClosing(WindowEvent arg0) {
super.windowClosing(arg0);
MainFrame.this.dispose();
System.exit(0);
}
}
public List getList() {
return list;
}
public TextField getTxtIP() {
return txtIP;
}
public TextField getTxtData() {
return txtData;
}
}
第二步:设计socket逻辑,本例中使用UDP协议:
package j2se.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
public class MessageTransform {
private MainFrame mainFrame;
private DatagramSocket dsSender;
private Thread receierThread;
private DatagramSocket dsReceiver;
private int PortReceiver = 3000;
private int bufLengthReceiver = 1024;
public MessageTransform(MainFrame mainFrame) {
this.mainFrame = mainFrame;
initData();
initSender();
initReceiver();
}
private void initData() {
try {
dsSender = new DatagramSocket();
dsReceiver = new DatagramSocket(PortReceiver);
} catch (SocketException e) {
e.printStackTrace();
}
}
private void initSender() {
mainFrame.getTxtData().addActionListener(this.new SenderAction());
}
private void initReceiver() {
receierThread = new Thread(this.new ReceiverAction());
receierThread.start();
}
private class SenderAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String sendData = mainFrame.getTxtData().getText();
if (sendData.isEmpty() || sendData.length() <= 0) {
return;
}
mainFrame.getList().add("您说: "+sendData,0);
byte[] buf = sendData.getBytes();
try {
DatagramPacket dp = new DatagramPacket(buf, buf.length,
InetAddress.getByName(mainFrame.getTxtIP().getText()),
PortReceiver);
dsSender.send(dp);
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
mainFrame.getTxtData().setText("");
}
}
private class ReceiverAction implements Runnable {
@Override
public void run() {
byte[] buf = new byte[bufLengthReceiver];
DatagramPacket dp = new DatagramPacket(buf, bufLengthReceiver);
while (true) {
try {
dsReceiver.receive(dp);
mainFrame.getList().add(new String(buf,0,dp.getLength())+" from:" + dp.getAddress().getHostAddress()+":"+dp.getPort(), 0);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
第三部:程序入口:
package j2se.gui;
public class Chat {
private MainFrame mainFrame;
private MessageTransform messageTransform;
public Chat() {
mainFrame = new MainFrame();
messageTransform = new MessageTransform(mainFrame);
}
public static void main(String[] args) {
new Chat();
}
}