java Jframe 聊天室服务器端半成品

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.io.*;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Color;
import javax.swing.SwingConstants;
import java.awt.Component;


public class ChatTest extends JFrame {
private static BufferedReader br=null;
private static PrintWriter pw=null;
private static ServerSocket ss; //定义一个ServerSocket,服务器用
private static Socket s; //定义一个Socket,与客户机联系用
static Scanner scanner=new Scanner(System.in);
private JPanel contentPane;
private JTextField textField;


/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ChatTest frame = new ChatTest();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}





/**
* Create the frame.
*/
public ChatTest() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 794, 440);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setResizable(false);
setTitle("Java Chating Room v1.0.0");


JTextArea textAreaMain = new JTextArea();
textAreaMain.setEditable(false);






JTextArea textAreaInpunt = new JTextArea();





JButton clear = new JButton("\u6E05\u7A7A");
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textAreaMain.setText(null);

}
});
clear.setFont(new Font("宋体", Font.BOLD, 15));

JLabel lblNewLabel = new JLabel("\u5DF2\u8FDE\u63A5IP\uFF1A");

JButton send = new JButton("\u53D1\u9001");
send.addActionListener(new ActionListener() {


public void actionPerformed(ActionEvent e) { //点发送按钮,发送服务器端文本框输入的内容

try{
 br=new BufferedReader(new InputStreamReader(s.getInputStream()));
pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
String str = textAreaInpunt.getText();
s.getOutputStream();
pw.println(str);
pw.flush();
pw.close();

}catch (Exception e1) {
e1.printStackTrace();
}


}
});
send.setFont(new Font("宋体", Font.BOLD, 15));

JButton start = new JButton("\u542F\u52A8");
start.addActionListener(new ActionListener() { //增加一个监听器
public void actionPerformed(ActionEvent e) {
textAreaMain.setText(textAreaMain.getText()+"启动服务器。。。"); //给文本框设置一个字符串
try {
ss=new ServerSocket(65530); //ServerSocket的实例,并设置端口
textAreaMain.setText(textAreaMain.getText()+"\n服务器正常启动。。。");
s=ss.accept();//阻塞方法,等待客户机连接
textAreaMain.setText(textAreaMain.getText()+"\n连接成功"+s.getRemoteSocketAddress()); //当客户机连接成功时,输出“连接成功”,并获取客户机地址
br=new BufferedReader(new InputStreamReader(s.getInputStream())); //获取客户机输入的内容
String string=br.readLine();
textAreaMain.setText(textAreaMain.getText()+"Server读到:"+string+"\nServer端请输入:");

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

}
});
start.setFont(new Font("宋体", Font.BOLD, 15));
start.setForeground(Color.GREEN);

JTextArea textAreaIP = new JTextArea();

JButton stop = new JButton("\u65AD\u5F00");
stop.setForeground(Color.RED);
stop.setFont(new Font("宋体", Font.BOLD, 15));

JLabel lblPort = new JLabel("port");
lblPort.setFont(new Font("宋体", Font.BOLD, 13));

textField = new JTextField();
textField.setColumns(10);
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textAreaMain, GroupLayout.PREFERRED_SIZE, 569, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblPort, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textField, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
.addComponent(lblNewLabel)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(start, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 39, Short.MAX_VALUE)
.addComponent(stop, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(textAreaInpunt, GroupLayout.PREFERRED_SIZE, 571, GroupLayout.PREFERRED_SIZE)
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addComponent(clear, GroupLayout.PREFERRED_SIZE, 114, GroupLayout.PREFERRED_SIZE)
.addComponent(send, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)))))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(597)
.addComponent(textAreaIP, GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)))
.addContainerGap())
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(textAreaMain, GroupLayout.PREFERRED_SIZE, 294, GroupLayout.PREFERRED_SIZE)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(lblNewLabel)
.addGap(9)
.addComponent(textAreaIP, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
.addGap(19)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(stop, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE)
.addComponent(start, GroupLayout.PREFERRED_SIZE, 54, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(lblPort)
.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED)))
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(send, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
.addGap(19)
.addComponent(clear)))
.addContainerGap(14, Short.MAX_VALUE))
.addGroup(gl_contentPane.createSequentialGroup()
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(textAreaInpunt, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))))
);
gl_contentPane.linkSize(SwingConstants.HORIZONTAL, new Component[] {clear, send});
contentPane.setLayout(gl_contentPane);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值