|
package tcpclient;
import javax.swing.UIManager;
import java.awt.*;
/**
* <p>Title: no</p>
* <p>Description: This a Java builder program</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: home</p>
* @author syd168
* @version 1.0
*/
public class Application1 {
private boolean packFrame = false;
//Construct the application
public Application1() {
Frame1 frame = new Frame1();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new Application1();
}
private void jbInit() throws Exception {
}
}
=================================
package tcpclient;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
/**
* <p>Title: no</p>
* <p>Description: This a Java builder program</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: home</p>
* @author syd168
* @version 1.0
*/
public class Frame1 extends JFrame implements Runnable{
private JPanel contentPane;
private TextArea textArea1 = new TextArea();
private Label label1 = new Label();
private JTextField textField1 = new JTextField();
private JButton button1 = new JButton();
private JButton button2 = new JButton();
private JButton button3 = new JButton();
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
JTextField server = new JTextField();
JLabel jLabel1 = new JLabel();
JLabel jLabel2 = new JLabel();
JTextField port = new JTextField();
JScrollPane jScrollPane1 = new JScrollPane();
JList jList1 = new JList();
JLabel jLabel3 = new JLabel();
DefaultListModel l1=new DefaultListModel();
//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
server.setText(InetAddress.getLocalHost().getHostAddress());
server.setBounds(new Rectangle(69, 252, 74, 22));
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
textArea1.setBackground(Color.orange);
textArea1.setEditable(false);
textArea1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 14));
textArea1.setBounds(new Rectangle(11, 9, 383, 199));
contentPane.setLayout(null);
this.setSize(new Dimension(506, 314));
this.setTitle("客户端");
label1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
label1.setText("消息:");
label1.setBounds(new Rectangle(26, 213, 32, 27));
button1.setActionCommand("发送");
button1.setBounds(new Rectangle(334, 216, 62, 23));
button1.setEnabled(false);
button1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
button1.setText("发送");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
button2.setBounds(new Rectangle(266, 251, 58, 24));
button2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
button2.setText("连接");
button2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
button3.setBounds(new Rectangle(333, 251, 62, 22));
button3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
button3.setText("退出");
button3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button3_actionPerformed(e);
}
});
jLabel1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel1.setToolTipText("");
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel1.setHorizontalTextPosition(SwingConstants.RIGHT);
jLabel1.setText("服务器:");
jLabel1.setBounds(new Rectangle(11, 254, 58, 20));
jLabel2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel2.setToolTipText("");
jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
jLabel2.setHorizontalTextPosition(SwingConstants.RIGHT);
jLabel2.setText("端口:");
jLabel2.setBounds(new Rectangle(172, 253, 27, 18));
port.setText("8000");
port.setBounds(new Rectangle(202, 251, 40, 22));
jList1.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jLabel3.setBounds(new Rectangle(406, 254, 91, 21));
textField1.setBounds(new Rectangle(58, 215, 271, 24));
jScrollPane1.setBounds(new Rectangle(404, 14, 94, 226));
jScrollPane1.getViewport().add(jList1);
contentPane.add(button3, null);
contentPane.add(button2, null);
contentPane.add(button1, null);
contentPane.add(server, null);
contentPane.add(jLabel1, null);
contentPane.add(textField1, null);
contentPane.add(textArea1, null);
contentPane.add(jLabel2, null);
contentPane.add(port, null);
contentPane.add(label1, null);
contentPane.add(jScrollPane1, null);
contentPane.add(jLabel3, null);
jList1.setModel(l1);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
//添加代码
try{
out.println("大家尽情聊,再见!");
out.flush();
}catch(Exception ex){}
finally{
System.exit(0);
}
}
}
void button1_actionPerformed(ActionEvent e) {
out.println(jLabel3.getText()+"对"+l1.getElementAt(jList1.getSelectedIndex())+"说:"+textField1.getText());
out.flush();
//textArea1.append("你对:"+l1.getElementAt(jList1.getSelectedIndex())+"说:"+textField1.getText()+"\n");
textField1.setText("");
}
void button3_actionPerformed(ActionEvent e) {
try{
out.println("大家尽情聊,再见!");
out.flush();
}catch(Exception e2){}
finally{
System.exit(0);
}
}
void button2_actionPerformed(ActionEvent e) {
Thread thread = new Thread(this);
thread.start();
}
public void run() {
try{
//在5438端口上打开到syd168的连接,说明它其实就是你的本地计算机名
//如果你的计算机的名称不是“syd168”,请修改为你的计算机名称
socket = new Socket(server.getText(),Integer.parseInt(port.getText()));
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));//接受输入的对象
out = new PrintWriter(socket.getOutputStream());//通过该对象输出信息
button1.setEnabled(true);//当连接成功的时候使它有效
receiver r = new receiver();//创建接收线程
Thread t = new Thread(r);
t.start();
textArea1.append("欢迎加入Java火星人聊天室(by syd168)...\n");
button2.setEnabled(false);
}catch(Exception e){
textArea1.append("连接失败!请确认服务器地址并且服务器已启动(syd168)."+"\n");
}
}
private class receiver implements Runnable{
public void run(){
String s1 = null;
try{
s1 = in.readLine();
while(s1!= "服务器断开!"){
if(s1.equals("Clear users!")){
l1.removeAllElements();
l1.addElement("大家");
jList1.setSelectedIndex(0);
}
else if(jLabel3.getText().equals("")){ jLabel3.setText(s1);}
else if(s1.length()<8&&!jLabel3.getText().equals(s1)){ l1.addElement(s1);}
else if(!jLabel3.getText().equals(s1)){textArea1.append(s1+"\n");}
s1 = in.readLine();
}
in.close();
out.close();
socket.close();
}catch(Exception e){}
button1.setEnabled(false); //服务器端已退出,禁止发送消息
}
}
} |
|