最近写了一段聊天室java代码
/* dkplus专业搜集和编写实用电脑软件教程,搜集各种软件资源和计算机周边(java网络编程,seo网站优化,web开发,lnmp,java网络编程,毕业论文设计),独立制作视频和ppt和音频微信公众号,点击进入 dkplus官方博客http://dkplus.iteye.com 微信搜索dkplus关注公众号可获取海量计算机周边资源。 */
package com.server;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.*;
public class ServerWindow extends JFrame{
@SuppressWarnings("unchecked")
public ServerWindow()
{
Server ss = new Server();
Thread t = new Thread(ss);
t.start();
dialogArea = new JTextArea(11,47);
dialogArea.setLineWrap(true);
dialogArea.setFont(messageFont);
dialogScrollPane = new JScrollPane(dialogArea);
dialogPanel = new JPanel();
dialogPanel.add(dialogScrollPane);
speedInput = new JComboBox(new String[]{
"What can I do for you, my son?",
"Oh,sorry,son,I cann't do that for you."
});
speedInput.setActionCommand("speedInput");
Action action = new Action();
speedInput.addActionListener(action);
comboxPanel = new JPanel();
comboxPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
comboxPanel.setBorder(BorderFactory.createEtchedBorder());
comboxPanel.add(speedInput);
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(dialogPanel,BorderLayout.CENTER);
inputArea = new JTextArea(11,47);
inputArea.setLineWrap(true);//设置自动换行
inputArea.setFont(messageFont);
inputPanel = new JPanel();
inputPanel.add(inputArea);
enterButton = new JButton("Enter");
enterButton.setActionCommand("enterButton");
enterButton.addActionListener(action);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(enterButton);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(comboxPanel,BorderLayout.NORTH);
panel2.add(inputPanel,BorderLayout.CENTER);
panel2.add(buttonPanel,BorderLayout.SOUTH);
setLayout(new GridLayout(2,1));
add(panel1);
add(panel2);
}
// class Server extends Thread{
class Server implements Runnable{
public void run(){
try {
try {
server = new ServerSocket(6666);
client = server.accept();
InputStream isr = client.getInputStream();
@SuppressWarnings("resource")
Scanner scanner = new Scanner(isr);
// while(scanner.hasNextLine())
// {
inputMessage = scanner.nextLine();
// }
// InputStreamReader isr = new InputStreamReader(client.getInputStream());
// BufferedReader bf = new BufferedReader(isr);
// inputMessage = bf.readLine();
dialogArea.append("Client:\n"+inputMessage);
System.out.println("服务器在监听");
pw = new PrintWriter(client.getOutputStream(),true);
// pw.println(clientMessage);
// outputMessage = inputArea.getText();
System.out.println("Stream output "+outputMessage+"\n");
// pw.println(outputMessage);
// pw.write(outputMessage);
}
finally{
// server.close();
// client.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
private ServerSocket server = null;
private Socket client = null;
}
class Action implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "enterButton"){
outputMessage = inputArea.getText();
System.out.println("outputMessageByenterButton"+outputMessage+"\n");//测试语句可以得到该语句
dialogArea.append("Server:\n"+outputMessage+"\n");
inputArea.setText(null);//将输入域清屏
}else if(e.getActionCommand() == "speedInput"){
outputMessage = (String)speedInput.getSelectedItem();
inputArea.setText(outputMessage);
System.out.println("inputAreaByspeedInput outputMessage = "+outputMessage+"\n");
pw.write(outputMessage);
}
}
}
private PrintWriter pw = null;
private static String inputMessage = null;//显示到dialogArea的信息
private static String outputMessage = null;
JTextArea dialogArea = null;
JTextArea inputArea = null;
JScrollPane dialogScrollPane = null;
JComboBox speedInput = null;
JButton enterButton = null;
JPanel dialogPanel = null,comboxPanel = null,inputPanel = null,buttonPanel = null;
JPanel panel1 = null,panel2 = null;
Font messageFont = new Font("仿宋",Font.BOLD,16);
// Font nameFont = new Font("仿宋",Font.BOLD,14);
}
/* dkplus专业搜集和编写实用电脑软件教程,搜集各种软件资源和计算机周边(java网络编程,seo网站优化,web开发,lnmp,java网络编程,毕业论文设计),独立制作视频和ppt和音频微信公众号,点击进入 dkplus官方博客http://dkplus.iteye.com 微信搜索dkplus关注公众号可获取海量计算机周边资源。 */
package com.server;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.*;
public class ServerWindow extends JFrame{
@SuppressWarnings("unchecked")
public ServerWindow()
{
Server ss = new Server();
Thread t = new Thread(ss);
t.start();
dialogArea = new JTextArea(11,47);
dialogArea.setLineWrap(true);
dialogArea.setFont(messageFont);
dialogScrollPane = new JScrollPane(dialogArea);
dialogPanel = new JPanel();
dialogPanel.add(dialogScrollPane);
speedInput = new JComboBox(new String[]{
"What can I do for you, my son?",
"Oh,sorry,son,I cann't do that for you."
});
speedInput.setActionCommand("speedInput");
Action action = new Action();
speedInput.addActionListener(action);
comboxPanel = new JPanel();
comboxPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
comboxPanel.setBorder(BorderFactory.createEtchedBorder());
comboxPanel.add(speedInput);
panel1 = new JPanel();
panel1.setLayout(new BorderLayout());
panel1.add(dialogPanel,BorderLayout.CENTER);
inputArea = new JTextArea(11,47);
inputArea.setLineWrap(true);//设置自动换行
inputArea.setFont(messageFont);
inputPanel = new JPanel();
inputPanel.add(inputArea);
enterButton = new JButton("Enter");
enterButton.setActionCommand("enterButton");
enterButton.addActionListener(action);
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(enterButton);
panel2 = new JPanel();
panel2.setLayout(new BorderLayout());
panel2.add(comboxPanel,BorderLayout.NORTH);
panel2.add(inputPanel,BorderLayout.CENTER);
panel2.add(buttonPanel,BorderLayout.SOUTH);
setLayout(new GridLayout(2,1));
add(panel1);
add(panel2);
}
// class Server extends Thread{
class Server implements Runnable{
public void run(){
try {
try {
server = new ServerSocket(6666);
client = server.accept();
InputStream isr = client.getInputStream();
@SuppressWarnings("resource")
Scanner scanner = new Scanner(isr);
// while(scanner.hasNextLine())
// {
inputMessage = scanner.nextLine();
// }
// InputStreamReader isr = new InputStreamReader(client.getInputStream());
// BufferedReader bf = new BufferedReader(isr);
// inputMessage = bf.readLine();
dialogArea.append("Client:\n"+inputMessage);
System.out.println("服务器在监听");
pw = new PrintWriter(client.getOutputStream(),true);
// pw.println(clientMessage);
// outputMessage = inputArea.getText();
System.out.println("Stream output "+outputMessage+"\n");
// pw.println(outputMessage);
// pw.write(outputMessage);
}
finally{
// server.close();
// client.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
private ServerSocket server = null;
private Socket client = null;
}
class Action implements ActionListener
{
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand() == "enterButton"){
outputMessage = inputArea.getText();
System.out.println("outputMessageByenterButton"+outputMessage+"\n");//测试语句可以得到该语句
dialogArea.append("Server:\n"+outputMessage+"\n");
inputArea.setText(null);//将输入域清屏
}else if(e.getActionCommand() == "speedInput"){
outputMessage = (String)speedInput.getSelectedItem();
inputArea.setText(outputMessage);
System.out.println("inputAreaByspeedInput outputMessage = "+outputMessage+"\n");
pw.write(outputMessage);
}
}
}
private PrintWriter pw = null;
private static String inputMessage = null;//显示到dialogArea的信息
private static String outputMessage = null;
JTextArea dialogArea = null;
JTextArea inputArea = null;
JScrollPane dialogScrollPane = null;
JComboBox speedInput = null;
JButton enterButton = null;
JPanel dialogPanel = null,comboxPanel = null,inputPanel = null,buttonPanel = null;
JPanel panel1 = null,panel2 = null;
Font messageFont = new Font("仿宋",Font.BOLD,16);
// Font nameFont = new Font("仿宋",Font.BOLD,14);
}