概述
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Client {
JTextArea jta;
JTextField jtf;
BufferedReader in;
PrintWriter out;
private void initGUI(){
JFrame f = new JFrame("Client");
f.setSize(400,300);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta = new JTextArea();
jta.setEditable(false);
f.add(new JScrollPane(jta));
jtf = new JTextField();
f.add(jtf,"South");
f.setVisible(true);
jtf.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = jtf.getText();
jtf.setText("");
out.println(text);
out.flush();
}
});
}
private void initNet(){
try{
Socket s = new Socket("127.0.0.1",8186);
out = new PrintWriter(s.getOutputStream());
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(Exception e){
e.printStackTrace();
}
}
private void receive(){
try{
while(true){
String str = in.readLine();
if(str==null){
return;
}
jta.append("˵"+str+"n");
}
}catch(Exception e){
e.printStackTrace();
}
}
private Client(){
this.initGUI();
this.initNet();
}
public static void main(String[] args) {
Client c = new Client();
c.receive();
}
}
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.ArrayList;
import java.util.List;
public class Server {
public static void main(String[] args) {
try{
ServerSocket ss = new ServerSocket(8186);
List sockets = new ArrayList();
while(true){
Socket s = ss.accept();
sockets.add(s);
Thread t1 = new ChatThread(s,sockets);
t1.start();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
class ChatThread extends Thread{
Socket s;
List sockets;
public ChatThread(Socket s,List sockets){
this.s = s;
this.sockets = sockets;
}
public void run(){
try{
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true){
String str = in.readLine();
for(int i=0;i
Socket s2 = (Socket)sockets.get(i);
PrintWriter out = new PrintWriter(s2.getOutputStream());
out.println(str);
out.flush();
}
}
}catch (Exception e) {}
finally{
try{
s.close();
}catch (Exception e) {}
}
}
}
最后
以上就是自由自行车为你收集整理的java重写窗口的叉点击_求助 java 如何编写JFrame窗体右上角红色打叉关闭按钮的事件?...的全部内容,希望文章能够帮你解决java重写窗口的叉点击_求助 java 如何编写JFrame窗体右上角红色打叉关闭按钮的事件?...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复