论坛首页 Java企业应用论坛

练手java GUI(一)

浏览 5252 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-11-28   最后修改:2010-11-29

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();
	}
}

 

 

   发表时间:2010-11-29  
上面的程序在有些机器上跑,显示出的是乱码,想知道应该怎么改??
0 请登录后投票
   发表时间:2010-11-29  
把中文全部写在properties里用unicode编码保存。。。要不然到Linux下还是乱码。。
0 请登录后投票
   发表时间:2010-11-30  
zgzdzh 写道
把中文全部写在properties里用unicode编码保存。。。要不然到Linux下还是乱码。。


十分感谢,回头试试。。
0 请登录后投票
   发表时间:2010-12-02  
能在局域网通信吗?
0 请登录后投票
   发表时间:2010-12-02  
sam_kee 写道
能在局域网通信吗?


可以~~
0 请登录后投票
   发表时间:2010-12-02  
qianhd 写道
玩具      


哈哈,过奖了,要是我自己评价,就是连玩具都不如,玩具最起码还是有一定的利用价值~~~
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics