求java高手解释下面程序的含义,说一下每一个函数的具体含义
客户端:importjava.io.*;importjava.text.*;importjava.awt.*;importjava.util.*;importjava.a...
客户端:
import java.io.*;
import java.text.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Client extends JFrame{
+ JTextArea jta;
private JTextField jtf;
private JButton jb;
private JLabel name;
private Socket s;
private PrintWriter pw;
private BufferedReader br;
private DateFormat format;
private Date date=new Date();
public Client(){
this.setSize(300,350);
this.setTitle("聊天室");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta=new JTextArea(15,20);
jtf=new JTextField(15);
jb=new JButton("发送");
name=new JLabel(" ");
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout());
jp.add(name);
jp.add(jtf);
jp.add(jb);
jb.addActionListener(new SendMessage());
this.setLayout(new BorderLayout());
this.add(jta,BorderLayout.CENTER);
this.add(jp,BorderLayout.NORTH);
getMessage();
this.setVisible(true);
}
public void getMessage(){
try{
s=new Socket("127.0.0.1",222);
pw=new PrintWriter(s.getOutputStream());
new ClientThread().start();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
name.setText(JOptionPane.showInputDialog(this,"请输入姓名"));
}
class ClientThread extends Thread{
public void run(){
while(true){
try{
br=new BufferedReader(
new InputStreamReader(s.getInputStream()));
jta.append(br.readLine()+"\n");
}catch (IOException e){
e.printStackTrace();
}
}
}
}
class SendMessage implements ActionListener{
public void actionPerformed(ActionEvent e){
format=DateFormat.getTimeInstance(DateFormat.LONG);
String time=format.format(date);
pw.println(name.getText()+" "+time+"说:\n"+" "+jtf.getText()+"\n");
pw.flush();
jtf.setText(" ");
}
}
public static void main(String args[]){
new Client();
}
} 展开
import java.io.*;
import java.text.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class Client extends JFrame{
+ JTextArea jta;
private JTextField jtf;
private JButton jb;
private JLabel name;
private Socket s;
private PrintWriter pw;
private BufferedReader br;
private DateFormat format;
private Date date=new Date();
public Client(){
this.setSize(300,350);
this.setTitle("聊天室");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta=new JTextArea(15,20);
jtf=new JTextField(15);
jb=new JButton("发送");
name=new JLabel(" ");
JPanel jp=new JPanel();
jp.setLayout(new FlowLayout());
jp.add(name);
jp.add(jtf);
jp.add(jb);
jb.addActionListener(new SendMessage());
this.setLayout(new BorderLayout());
this.add(jta,BorderLayout.CENTER);
this.add(jp,BorderLayout.NORTH);
getMessage();
this.setVisible(true);
}
public void getMessage(){
try{
s=new Socket("127.0.0.1",222);
pw=new PrintWriter(s.getOutputStream());
new ClientThread().start();
}catch(UnknownHostException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
name.setText(JOptionPane.showInputDialog(this,"请输入姓名"));
}
class ClientThread extends Thread{
public void run(){
while(true){
try{
br=new BufferedReader(
new InputStreamReader(s.getInputStream()));
jta.append(br.readLine()+"\n");
}catch (IOException e){
e.printStackTrace();
}
}
}
}
class SendMessage implements ActionListener{
public void actionPerformed(ActionEvent e){
format=DateFormat.getTimeInstance(DateFormat.LONG);
String time=format.format(date);
pw.println(name.getText()+" "+time+"说:\n"+" "+jtf.getText()+"\n");
pw.flush();
jtf.setText(" ");
}
}
public static void main(String args[]){
new Client();
}
} 展开
3个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询