一个更加漂亮的GUI界面网络通信程序
客户机端:
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class Client extends JFrame
{
Container contentPanel = this.getContentPane();
JButton connectJButton =new JButton("连接服务器");
JButton closeJButton = new JButton("关闭对话");
JButton fireJButton = new JButton("发送信息");
JButton rushJButton = new JButton("刷新显示器");
JTextField inputJText = new JTextField(15);
JTextArea displayJText = new JTextArea();
JScrollPane containPane = new JScrollPane(displayJText);
JPanel jbuttonJPanel = new JPanel();
JLabel dialogueJLabel = new JLabel("客 户 端 显 示 器",JLabel.CENTER);
JLabel stateJLabel = new JLabel("请连接服务器");
JPanel jlabelJPanel = new JPanel();
JTextField inputInetJText = new JTextField(15);
JLabel injtJLabel = new JLabel("服务器名称:");
JPanel workJPanel = new JPanel();
Font font = new Font("宋体",Font.BOLD,22);
//网络对象声明
Socket clientSocket = null;//客户端套接字
BufferedReader clientReader = null;//客户端读出流
PrintWriter clientWriter = null;//客户端写入流
ClientReaderThread crt =new ClientReaderThread();
Thread crThread =null; //客户端读出线程
public Client()
{
jInit();
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
this.setTitle("客户通讯器");
this.setSize(650,250);
this.setVisible(true);
}
private void jInit()
{
contentPanel.setLayout(new BorderLayout(10,10));
contentPanel.add(jlabelJPanel,BorderLayout.NORTH);
jlabelJPanel.add(dialogueJLabel);
jlabelJPanel.add(injtJLabel);
jlabelJPanel.add(inputInetJText);
dialogueJLabel.setFont(font);
contentPanel.add(new JLabel(" "),BorderLayout.EAST);
contentPanel.add(new JLabel(" "),BorderLayout.WEST);
contentPanel.add(workJPanel,BorderLayout.CENTER);
contentPanel.add(stateJLabel,BorderLayout.SOUTH);
workJPanel.setLayout(new BorderLayout());
workJPanel.add(containPane,BorderLayout.CENTER);
workJPanel.add(jbuttonJPanel,BorderLayout.SOUTH);
jbuttonJPanel.add(connectJButton);
connectJButton.addActionListener(new ConnectJButton_ActionListener());
jbuttonJPanel.add(closeJButton);
closeJButton.addActionListener(new CloseJButton_ActionListener());
jbuttonJPanel.add(fireJButton);
jbuttonJPanel.add(inputJText);
jbuttonJPanel.add(rushJButton);
fireJButton.addActionListener(new FireJButton_ActionListener());
rushJButton.addActionListener(new RushJButton_ActionListener());
}
/**
*
*连接服务器,初始所有网络对象。
*
*/
private void connectServer()
{
try
{
if (clientSocket == null)
{
clientSocket = new Socket(inputInetJText.getText(),1800);
clientReader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
clientWriter = new PrintWriter(clientSocket.getOutputStream(),true);
Thread crThread = new Thread(crt) ;
crt.bCRT = true;
crThread.start(); //运行客户端读出线程
stateJLabel.setText("服务器成功连接,已经获取客户端读出流,写入流。");
}
else
{
stateJLabel.setText("服务器已经连接");
}
}
catch(UnknownHostException e)
{
closeServer();
stateJLabel.setText("不存在此服务器");
}
catch(IOException e)
{
closeServer();
stateJLabel.setText("网络中断,连接服务器不成功,服务器末打开。");
}
}
/**
*断开服务器,关闭所有网络对象。
*/
private void closeServer()
{
try
{
if (clientSocket != null)
{
clientSocket.close();
clientSocket= null;
if (clientReader != null)
{
clientReader.close();
clientReader= null;
}
if (clientWriter != null)
{
clientWriter.close();
clientWriter = null;
}
stateJLabel.setText("服务器成功断开");
}
else
{
stateJLabel.setText("服务器已经断开");
}
}
catch(IOException e)
{
clientReader = null;
clientWriter = null;
clientSocket = null;
stateJLabel.setText("网络中断,服务器已经断开");
}
}
/**
*向服务器发送消息
*/
private void fireMessage()
{
try
{
String temp = inputJText.getText();
if (temp .equals(""))
{
stateJLabel.setText("发送信息不能为空");
return;
}
else
{
if(clientWriter != null)
{
clientWriter.println("客户端说: " + temp);
displayJText.setText(displayJText.getText() + "客户端说: "+
inputJText.getText() + "\n");
clientWriter.flush();
stateJLabel.setText("消息成功发送");
}
else
{
stateJLabel.setText("还没有连接到服务器");
}
}
}
catch(Exception e)
{
stateJLabel.setText("网络中断,服务器关闭");
}
}
//客户端读出线程类
class ClientReaderThread implements Runnable
{
public boolean bCRT = true;
String temp = "";
public void run()
{
while (bCRT)
{
try
{
try
{
temp = clientReader.readLine();
}
//网络中断,自身关闭。
catch(IOException e)
{
closeServer();
closeJButton.setEnabled(true);
stateJLabel.setText("网络中断,自身关闭,服务器断开。或服务器直接断开。");
bCRT = false;
return;
}
if (temp == null)
{
closeServer();
stateJLabel.setText("网络中断,服务器关闭,自身断开。");
closeJButton.setEnabled(true);
bCRT = false;
return;
}
displayJText.setText(displayJText.getText() + temp + "\n");
}
catch(Exception e)
{
}
}
}
}
/*************控件监视器类*********************************************/
class CloseJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
crt.bCRT = false;//关闭客户端读出线程类
closeServer();
}
}
class ConnectJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
connectServer();
}
}
class FireJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
fireMessage();
}
}
class RushJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
displayJText.setText("");
stateJLabel.setText("刷新屏幕。");
}
}
public static void main(String args[])
{
new Client();
}
}
服务器端:
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
import com.sun.java.swing.plaf.motif.MotifLookAndFeel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import javax.swing.plaf.metal.MetalLookAndFeel;
/*******服务器运行步骤**********
* 1:建立服务器套接字,运行客户端搜索线程。
* 2:通过线程同步搜索客户端,然后初始客户端读出流,客户端写入流,运行客户端读出线程。
* 3:运行客户端读出线程,同时可向客户端发送消息。
* 4:随时可以关闭服务器。
*
*
**/
class Server extends JFrame
{
Container contentPanel = this.getContentPane();
JTextArea displayJText = new JTextArea();
JScrollPane containPane = new JScrollPane(displayJText);
JButton openJButton = new JButton("开启服务器");
JButton closeJButton = new JButton("关闭服务器");
JButton fireJButton = new JButton("发送");
JButton rushJButton = new JButton("刷新显示区");
JTextField inputJText = new JTextField(15);
JLabel stateJLabel = new JLabel("请开启服务器");
JPanel jbuttonJPanel = new JPanel();
JLabel dialogueJLabel=new JLabel("服 务 器 对 话 显 示 区",JLabel.CENTER);
JPanel workJPanel = new JPanel();
JButton closeReader = new JButton("关闭读出");
JRadioButton metalJRadio = new JRadioButton("metal");
JRadioButton windowJRadio = new JRadioButton("window");
JRadioButton motifJRadio = new JRadioButton("motif");
ButtonGroup jradioBGroup = new ButtonGroup();
JPanel southJPanel= new JPanel();
JLabel jradioJLabel = new JLabel("请选择不同风格:");
JPanel jradioJPanel =new JPanel();
JRadio_ItemLisener jritemListener = new JRadio_ItemLisener();
Font font = new Font("宋体",Font.BOLD,22);
//网络对象声明。
ServerSocket serverSocket = null;//服务器套接字。
Socket clientSocket = null;//客户端套接字。
BufferedReader clientReader = null;//客户端读出流。
PrintWriter clientWriter = null;//客户端写入流。
FindClientSocket fcs = new FindClientSocket();
Thread fcsThread = null ;//客户端搜索线程。
ClientReaderThread crt =new ClientReaderThread();
Thread crThread = null ;//客户端读出线程。
public Server()
{
jInit();
this.setTitle("通讯服务器");
this.setSize(650,250);
this.setVisible(true);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
/**
*加载界面控件。
*/
private void jInit()
{
contentPanel.setLayout(new BorderLayout(10,10));
contentPanel.add(dialogueJLabel,BorderLayout.NORTH);
dialogueJLabel.setFont(font);
contentPanel.add(new JLabel(" "),BorderLayout.EAST);
contentPanel.add(new JLabel(" "),BorderLayout.WEST);
contentPanel.add(workJPanel,BorderLayout.CENTER);
contentPanel.add(southJPanel,BorderLayout.SOUTH);
southJPanel.setLayout(new BorderLayout());
southJPanel.add(jradioJPanel,BorderLayout.CENTER);
southJPanel.add(stateJLabel,BorderLayout.SOUTH);
jradioJPanel.add(jradioJLabel);
jradioJPanel.add(metalJRadio);
metalJRadio.addItemListener(jritemListener);
jradioJPanel.add(windowJRadio);
windowJRadio.addItemListener(jritemListener);
jradioJPanel.add(motifJRadio);
motifJRadio.addItemListener(jritemListener);
jradioBGroup.add(metalJRadio);
jradioBGroup.add(windowJRadio);
jradioBGroup.add(motifJRadio);
workJPanel.setLayout(new BorderLayout());
workJPanel.add(containPane,BorderLayout.CENTER);
workJPanel.add(jbuttonJPanel,BorderLayout.SOUTH);
jbuttonJPanel.add(openJButton);
openJButton.addActionListener(new OpenJButton_ActionListener());
jbuttonJPanel.add(closeJButton);
closeJButton.addActionListener(new CloseJButton_ActionListener());
jbuttonJPanel.add(fireJButton);
fireJButton.addActionListener(new FireJButton_ActionListener());
jbuttonJPanel.add(inputJText);
jbuttonJPanel.add(rushJButton);
rushJButton.addActionListener(new RushJButton_ActionListener());
}
/**
*开启服务器,初始所有网络对象。
*
*/
private void openServer()
{
if (serverSocket == null)
{
try
{
serverSocket = new ServerSocket(1800);//初始服务器套接字。
fcsThread =new Thread(fcs); //运行客户端搜索线,初始客户端套接字。
fcsThread.start();
}
catch(IOException e)
{
closeServer();
stateJLabel.setText("网络中断,服务器失败开启.");
}
}
else if (serverSocket != null & clientSocket != null)
{
stateJLabel.setText("服务器已经开启");
}
}
/**
*
*关闭服务器套接字,客户端套接字,客户端读出流,客户端写入流.
*/
private void closeServer()
{
if (serverSocket != null)
{
try
{
serverSocket.close();
serverSocket = null;
if (clientSocket != null)
{
clientSocket.close();
clientSocket=null;
}
if (clientReader != null)
{
clientReader.close();
clientReader = null;
}
if (clientWriter != null)
{
clientWriter.close();
clientWriter = null;
}
stateJLabel.setText("服务器成功关闭");
}
catch(IOException e)
{
clientReader = null;
clientWriter = null;
clientSocket = null;
serverSocket = null;
stateJLabel.setText("网络中断,服务器关闭.");
}
catch(Exception e)
{
stateJLabel.setText("有问题。 ");
}
}
else
{
stateJLabel.setText("服务器已经关闭");
}
}
/**
*向客户端发送消息
*/
private void fireMessage()
{
try
{
String temp = inputJText.getText();
if (temp.equals(""))
{
stateJLabel.setText("发送信息不能为空");
return;
}
else
{
if(clientWriter != null)
{
clientWriter.println("服务器说: " + temp);
displayJText.setText(displayJText.getText() + "服务器说: "+
inputJText.getText()+"\n");
clientWriter.flush();
stateJLabel.setText("消息成功发送");
}
else
{
stateJLabel.setText("还没有搜索到客户端");
}
}
}
catch(Exception e)
{
}
}
//客户端搜索线程类。
class FindClientSocket implements Runnable
{
public void run()
{
try
{
stateJLabel.setText("正在搜索客户端");
clientSocket = serverSocket.accept();
//搜索成功后,立即初始化客户端读出流,客户端写入流。
clientReader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
clientWriter = new PrintWriter(clientSocket.getOutputStream(),true);
Thread crThread = new Thread(crt) ;
crt.bCRT= true;
crThread.start(); //运行客户端读出线程
stateJLabel.setText("服务器成功开启,已经成功搜索到客户端");
}
catch(IOException e)
{
//网络中断,搜索客户端不成功,关闭所有网络对象。
closeServer();
stateJLabel.setText("网络中断,服务器关闭,搜索客户端失败");
}
}
}
//客户端读出线程类。
class ClientReaderThread implements Runnable
{
public boolean bCRT = true;
String temp = "";
public void run()
{
while (bCRT)
{
try
{
try
{
temp = clientReader.readLine();//从客户端读出信息。
}
catch(IOException e)//如果自身关闭,则停止线程。
{
closeServer();
stateJLabel.setText("网络中断,自身关闭,客户端断开。或客户端直接断开。");
return;
}
//当客户端关闭客户端套接字时,关闭所有网络对象。
if (temp == null)//如果客户端关闭,则停止线程。
{
closeServer();
stateJLabel.setText("网络中断,客户端关闭,自身断开。");
return;
}
//显示信息。
displayJText.setText(displayJText.getText() + temp + "\n");
}
catch(Exception e)
{
}
}
}
}
/*************控件监视器类*********************************************/
class OpenJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
openServer();
}
}
class CloseJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
crt.bCRT = false;
closeServer();
}
}
class FireJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
fireMessage();
}
}
class RushJButton_ActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
displayJText.setText("");
stateJLabel.setText("刷新屏幕。");
}
}
class JRadio_ItemLisener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object jr = e.getItem();
try
{
if (jr.equals(metalJRadio) & e.getStateChange()==e.SELECTED )
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
else if(jr.equals(motifJRadio) & e.getStateChange()==e.SELECTED)
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
}
else if (jr.equals(windowJRadio) & e.getStateChange()==e.SELECTED)
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
}
catch(Exception ex)
{
}
SwingUtilities.updateComponentTreeUI(getContentPane());
}
}
public static void main(String args[])
{
new Server();
}
}
[此贴子已经被作者于2010-12-12 08:05:00编辑过]