package ltserver;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.util.Date;
import javax.swing.JScrollPane;
import java.net.InetAddress;
import java.net.Socket;
import java.io.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2006</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class frmClient extends JFrame implements Runnable{
JPanel contentPane;
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField txtServer = new JTextField();
JTextField txtPort = new JTextField();
JButton btnConnect = new JButton();
JButton btnExit = new JButton();
JTextArea txtContent = new JTextArea();
JLabel jLabel3 = new JLabel();
JTextField txtMessage = new JTextField();
Socket sckClient=null;
InetAddress ClientIp=null;
JButton jButton1 = new JButton();
public frmClient() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
Thread t=new Thread(this);
t.start();
} catch (Exception exception) {
exception.printStackTrace();
}
}
public void run()
{
while(true)
{
try
{
try
{
DataInputStream myin=new DataInputStream(sckClient.getInputStream());
String strtemp=myin.readUTF();
txtContent.setText(txtContent.getText()+strtemp);
}
catch(Exception ae)
{
//ae.printStackTrace();
}
}
catch(Exception ae)
{
}
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
jLabel1.setText("服务器:");
jLabel1.setBounds(new Rectangle(16, 4, 54, 25));
jLabel2.setToolTipText("");
jLabel2.setText("端口号:");
jLabel2.setBounds(new Rectangle(15, 26, 57, 20));
txtServer.setText("zhlb");
txtServer.setBounds(new Rectangle(77, 4, 198, 24));
txtPort.setText("2000");
txtPort.setBounds(new Rectangle(77, 29, 198, 24));
btnConnect.setBounds(new Rectangle(290, 2, 81, 25));
btnConnect.setText("连接");
btnConnect.addActionListener(new frmClient_btnConnect_actionAdapter(this));
btnExit.setBounds(new Rectangle(290, 28, 81, 25));
btnExit.setText("退出");
btnExit.addActionListener(new frmClient_btnExit_actionAdapter(this));
txtContent.setText("");
//txtContent.setBounds(new Rectangle(14, 60, 358, 149));
JScrollPane scroll=new JScrollPane(txtContent);
scroll.setBounds(new Rectangle(14, 60, 358, 149));
jLabel3.setText("发送");
jLabel3.setBounds(new Rectangle(15, 223, 42, 25));
txtMessage.setText("");
txtMessage.setBounds(new Rectangle(62, 221, 310, 25));
txtMessage.addKeyListener(new frmClient_txtMessage_keyAdapter(this));
//contentPane.add(txtContent);
contentPane.add(scroll);
contentPane.add(txtPort);
contentPane.add(jLabel1);
contentPane.add(jLabel2);
contentPane.add(txtServer);
contentPane.add(jLabel3);
contentPane.add(txtMessage);
contentPane.add(btnExit);
contentPane.add(btnConnect);
contentPane.add(jButton1);
this.setVisible(true);
}
public void btnExit_actionPerformed(ActionEvent e) {
System.exit(0);
}
public void txtMessage_keyPressed(KeyEvent e) {
if (e.getKeyCode()==10)
{
Date now=new Date();
String strtemp="客户端"+" "+now.toLocaleString()+"\n"+txtMessage.getText()+"\n";
txtContent.setText(txtContent.getText()+strtemp);
txtMessage.setText("");
try
{
DataOutputStream myout=new DataOutputStream(sckClient.getOutputStream());
myout.writeUTF(strtemp);
myout.flush();
myout.close();
}
catch(Exception eee)
{
}
}
}
public void btnConnect_actionPerformed(ActionEvent e) {
try
{
InetAddress serverIp = InetAddress.getByName(txtServer.getText());
sckClient=new Socket(serverIp,Integer.parseInt(txtPort.getText()));
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String args[])
{
new frmClient();
}
}
class frmClient_txtMessage_keyAdapter extends KeyAdapter {
private frmClient adaptee;
frmClient_txtMessage_keyAdapter(frmClient adaptee) {
this.adaptee = adaptee;
}
public void keyPressed(KeyEvent e) {
adaptee.txtMessage_keyPressed(e);
}
}
class frmClient_btnConnect_actionAdapter implements ActionListener {
private frmClient adaptee;
frmClient_btnConnect_actionAdapter(frmClient adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnConnect_actionPerformed(e);
}
}
class frmClient_btnExit_actionAdapter implements ActionListener {
private frmClient adaptee;
frmClient_btnExit_actionAdapter(frmClient adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnExit_actionPerformed(e);
}
}
评论0