求解答_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 求解答

求解答

 2011/10/7 7:55:08  镜花水月两样情1990  http://562980080-qq-com.iteye.com  我要评论(0)
  • 摘要://客户端packagecom.xinzhan.demotest;importjava.awt.Color;importjava.awt.Font;importjava.awt.SystemColor;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.BufferedReader;importjava.io.IOException;importjava.io
  • 标签:
//客户端
package com.xinzhan.demotest;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class Client extends JFrame implements ActionListener{

private final JLabel portLabel = new JLabel();
private final JLabel portLabel_1 = new JLabel();
private final JTextField textField = new JTextField();
private final JLabel portLabel_2 = new JLabel();
private final JTextField textField_2 = new JTextField();
private final JLabel portLabel_3 = new JLabel();
private final JButton connecteButton = new JButton();
private final JButton stopButton = new JButton();
private final JScrollPane scrollPane = new JScrollPane();
private final JTextArea textArea = new JTextArea();
private final JTextField textField_1 = new JTextField();
private final JButton sendButton = new JButton();


private static BufferedReader reader;
private static PrintWriter writer;
private static Socket socket;
private static String message;
private static String data;


public static void main(String args[]) {
try {
Client frame = new Client();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public Client() {
super();
setBounds(100, 100, 500, 441);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().setFont(new Font("@楷体", Font.PLAIN, 20));
getContentPane().setBackground(UIManager.getColor("CheckBoxMenuItem.acceleratorForeground"));
setTitle("Client");

getContentPane().add(portLabel);
portLabel.setHorizontalAlignment(SwingConstants.CENTER);
portLabel.setFont(new Font("@楷体", Font.PLAIN, 20));
portLabel.setText("Port");
portLabel.setBounds(39, 36, 86, 27);

getContentPane().add(portLabel_1);
portLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
portLabel_1.setFont(new Font("Dialog", Font.PLAIN, 20));
portLabel_1.setText("connecte");
portLabel_1.setBounds(39, 87, 86, 27);

getContentPane().add(textField);
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setText("8888");
textField.setBackground(SystemColor.textHighlight);
textField.setFont(new Font("@楷体", Font.PLAIN, 18));
textField.setBounds(131, 28, 111, 35);

getContentPane().add(portLabel_2);
portLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
portLabel_2.setFont(new Font("Dialog", Font.PLAIN, 20));
portLabel_2.setText("Ip");
portLabel_2.setBounds(254, 36, 86, 27);

getContentPane().add(textField_2);
textField_2.setText("127.0.0.1");
textField_2.setHorizontalAlignment(SwingConstants.CENTER);
textField_2.setFont(new Font("Dialog", Font.PLAIN, 18));
textField_2.setBackground(SystemColor.textHighlight);
textField_2.setBounds(346, 30, 111, 35);

getContentPane().add(portLabel_3);
portLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
portLabel_3.setFont(new Font("Dialog", Font.PLAIN, 20));
portLabel_3.setText("stop");
portLabel_3.setBounds(254, 90, 86, 27);


getContentPane().add(scrollPane);
scrollPane.setBounds(25, 134, 432, 220);

scrollPane.setViewportView(textArea);
textArea.setFont(new Font("@楷体", Font.PLAIN, 18));

getContentPane().add(textField_1);
textField_1.setFont(new Font("@楷体", Font.PLAIN, 15));
textField_1.setBounds(25, 360, 348, 33);

getContentPane().add(sendButton);
sendButton.setBorder(new EmptyBorder(0, 0, 0, 0));
sendButton.setFont(new Font("@楷体", Font.PLAIN, 20));
sendButton.setText("send");
sendButton.setBounds(379, 360, 79, 33);
sendButton.addActionListener(this);

getContentPane().add(stopButton);
stopButton.setFont(new Font("@楷体", Font.PLAIN, 20));
stopButton.setBorder(new EmptyBorder(0, 0, 0, 0));
stopButton.setText("stop");
stopButton.setBounds(346, 86, 111, 35);
stopButton.addActionListener(this);

getContentPane().add(connecteButton);
connecteButton.setFont(new Font("@楷体", Font.PLAIN, 20));
connecteButton.setBorder(new EmptyBorder(0, 0, 0, 0));
connecteButton.setText("connecte");
connecteButton.setBounds(131, 82, 111, 35);
connecteButton.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==connecteButton){
connceteServer();
}
if(e.getSource()==sendButton){
sendMessage(message);
}
if(e.getSource()==this.stopButton){
System.exit(0);
}
}
public void connceteServer(){
try {
socket = new Socket("127.0.0.1",8888);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream());
writer.println("已经连接上");
data = reader.readLine();
//this.textArea.append(data+"\n");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void sendMessage(String message){
message = this.textField_1.getText().trim();
if(message==null||message.equals("")){
JOptionPane.showMessageDialog(null, "发送消息不能为空");
}
try {
writer = new PrintWriter(socket.getOutputStream());
writer.println("客户端说:"+message);
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
//服务器

package com.xinzhan.demotest;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

public class Server extends JFrame implements ActionListener{

private final JTextField textField = new JTextField();
private final JLabel portLabel = new JLabel();
private final JLabel portLabel_1 = new JLabel();
private final JTextField textField_2 = new JTextField();
private final JButton startButton = new JButton();
private final JLabel portLabel_2 = new JLabel();
private final JButton stopButton = new JButton();
private final JLabel portLabel_3 = new JLabel();
private final JScrollPane scrollPane = new JScrollPane();
private final JTextArea textArea = new JTextArea();
private final JTextField textField_1 = new JTextField();
private final JButton sendButton = new JButton();

private static BufferedReader reader;
private static PrintWriter writer;
private static ServerSocket server;
private static Socket socket;
private static String message;
private static String data;
private Vector<Socket> v = new Vector<Socket>();//保存所有客户端

public static void main(String args[]) {
try {
Server frame = new Server();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}

public Server() {
super();
setBounds(100, 100, 514, 440);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
getContentPane().setBackground(SystemColor.activeCaption);
setTitle("Server");

getContentPane().add(textField);
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setText("8888");
textField.setBackground(new Color(255, 255, 0));
textField.setFont(new Font("@楷体", Font.PLAIN, 20));
textField.setBounds(129, 25, 107, 30);

getContentPane().add(portLabel);
portLabel.setHorizontalAlignment(SwingConstants.CENTER);
portLabel.setHorizontalTextPosition(SwingConstants.CENTER);
portLabel.setFont(new Font("@楷体", Font.PLAIN, 20));
portLabel.setText("Port");
portLabel.setBounds(10, 26, 87, 28);
portLabel.setHorizontalTextPosition(JLabel.CENTER);

getContentPane().add(portLabel_1);
portLabel_1.setHorizontalTextPosition(SwingConstants.CENTER);
portLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
portLabel_1.setFont(new Font("Dialog", Font.PLAIN, 20));
portLabel_1.setText("Ip");
portLabel_1.setBounds(242, 28, 87, 28);

getContentPane().add(textField_2);
textField_2.setHorizontalAlignment(SwingConstants.CENTER);
textField_2.setText("127.0.0.1");
textField_2.setBackground(new Color(255, 255, 0));
textField_2.setFont(new Font("Dialog", Font.PLAIN, 20));
textField_2.setBounds(352, 24, 119, 30);

getContentPane().add(portLabel_2);
portLabel_2.setHorizontalTextPosition(SwingConstants.CENTER);
portLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
portLabel_2.setFont(new Font("Dialog", Font.PLAIN, 20));
portLabel_2.setText("Start");
portLabel_2.setBounds(10, 78, 87, 28);

getContentPane().add(portLabel_3);
portLabel_3.setHorizontalTextPosition(SwingConstants.CENTER);
portLabel_3.setHorizontalAlignment(SwingConstants.CENTER);
portLabel_3.setFont(new Font("Dialog", Font.PLAIN, 20));
portLabel_3.setText("stop");
portLabel_3.setBounds(242, 78, 87, 28);

getContentPane().add(scrollPane);
scrollPane.setBounds(31, 128, 440, 219);

scrollPane.setViewportView(textArea);
textArea.setFont(new Font("@楷体", Font.PLAIN, 15));

getContentPane().add(textField_1);
textField_1.setFont(new Font("@楷体", Font.PLAIN, 18));
textField_1.setBounds(31, 353, 343, 39);

getContentPane().add(sendButton);
sendButton.setBorder(new EmptyBorder(0, 0, 0, 0));
sendButton.setFont(new Font("@楷体", Font.PLAIN, 20));
sendButton.setText("send");
sendButton.setBounds(380, 353, 91, 39);
sendButton.addActionListener(this);

getContentPane().add(stopButton);
stopButton.setBorder(new EmptyBorder(0, 0, 0, 0));
stopButton.setFont(new Font("@楷体", Font.PLAIN, 20));
stopButton.setText("stop");
stopButton.setBounds(352, 73, 119, 38);
stopButton.addActionListener(this);


getContentPane().add(startButton);
startButton.setBorder(new EmptyBorder(0, 0, 0, 0));
startButton.setFont(new Font("@楷体", Font.PLAIN, 20));
startButton.setText("Start");
startButton.setBounds(130, 73, 106, 38);
startButton.addActionListener(this);
//startServer();

}

public void actionPerformed(ActionEvent e) {
if(e.getSource()==startButton){
startServer();
}
if(e.getSource()==sendButton){
sendMessage(message);
}
if(e.getSource()==stopButton){
System.exit(0);
}
}
public void startServer(){
try {
server = new ServerSocket(8888);
this.textArea.append("服务器已经启动!");
socket = server.accept();
writer = new PrintWriter(socket.getOutputStream(),true);
writer.println("已连接成功!");
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
data = reader.readLine();
// while(!data.equalsIgnoreCase("bye")){
// this.textArea.append(data+"\n");
// }


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public void sendMessage(String message){
message = this.textField_1.getText().trim();
if(message==null||message.equals("")){
JOptionPane.showMessageDialog(null, "发送消息不能为空");
}
try {
writer = new PrintWriter(socket.getOutputStream());
writer.println("服务器说:"+message);
writer.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
这是我写的一个单线程的聊天室,但是怎么一运行起来,窗口就不动了,求高手解答,急用
  • 相关文章
发表评论
用户名: 匿名