服务器端:
package HomeWork;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class singlechat extends JFrame {
JTextArea textarea,textarea1;
//JTextField textfield;
JButton button,button1,button2,button3,button4,button5;
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JScrollPane scrollpane;
public DataInputStream din;
public DataOutputStream dout;
public Socket socket ;
public static void main(String[] args) {
new singlechat();
}
public singlechat() {
super("聊天室");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try{
din.close();
dout.close();
socket.close();
System.exit(0);
}
catch (IOException ex){
System.out.println("client closing: "+ ex.toString());
}
}
});
//this.setLayout();
Font largeFont = new Font("TimesRoman", Font.BOLD, 20);
Border lineBorder = new LineBorder(Color.BLACK, 2);
button = new JButton("发送");
button1 = new JButton("老大");
button2 = new JButton("老二");
button3 = new JButton("老三");
button4 = new JButton("老四");
button5 = new JButton("老五");
textarea = new JTextArea(6, 30);
textarea1 = new JTextArea(3,30);
textarea.setForeground(Color.blue);
textarea.setFont(largeFont);
textarea.setBorder(lineBorder);
textarea1.setForeground(Color.red);
textarea1.setFont(largeFont);
textarea1.setBorder(lineBorder);
textarea1.setBackground(Color.cyan );
scrollpane = new JScrollPane(textarea);
//scrollpane.setPreferredSize(new Dimension(200, 200));
//textfield = new JTextField(20);//textfield要先new出来
//textfield.setForeground(Color.red);
// setContentPane(panel1);
panel1.setPreferredSize(new Dimension(400, 300));
//panel1.setBackground(Color.white);
// panel1.setForeground(Color.blue);
panel1.setVisible(true);
panel1.setBorder(new TitledBorder("显示区域"));
panel1.setLayout(new BorderLayout());// 设置边界布局
panel1.add(scrollpane, BorderLayout.CENTER);
//this.add(panel1);
// setContentPane(panel2);
//panel2.setBackground(Color.white);
//panel2.setForeground(Color.blue);
panel2.setVisible(true);
panel2.setPreferredSize(new Dimension(400, 100));
panel2.setBorder(new TitledBorder("输入区域"));
panel2.setLayout(new GridLayout(2,1,10,10));// 设置边界布局