同上 谢谢
解决方案 »
- 关于用Java构建Hashtable
- 求助 安装jdk时运行java正常 javac时显示非内部和外部命令
- System.gc(); 与Runtime.gc()的区别
- 能被3整除,且至少有一位数字是5的所有整数
- 请问ArrayList可以存储简单数据类型吗?
- 各位大虾帮我看下有什么好点的方法从33个数中选6组合,并放到一个数组中
- 谁有JDK1.4中文的API文档,谢谢!!
- 请教最简单的SERVLET应该如何运行?
- java怎么实现多台打印机的群集打印(帮忙up)
- 简说JAVA8引入函数式的问题
- 请问,面向对像的思想,怎样建立?怎样把面向对像的思想用在程序设计中?谢谢了
- 200分求LAN聊天室程序1
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;import javax.swing.*;public class MyChat{
JFrame frame;
Container con;
JPanel panel;
JTextArea textArea;
JLabel label;
JTextField textField;
JScrollPane scrollPane;
JButton addButton;
JButton clearButton;
Socket s;
BufferedReader in;
PrintWriter out;
String textRe;
boolean isStart ;
static String clientName ;
public static void main(String[] args)throws Exception {
clientName = args[0];
new MyChat().init();
} public void init()throws Exception{
frame = new JFrame("MyChat");
con = frame.getContentPane();
panel = new JPanel();
textArea = new JTextArea();
textArea.setEditable(false);
scrollPane = new JScrollPane(textArea);
label = new JLabel("please input:");
textField = new JTextField();
addButton = new JButton("OK");
clearButton = new JButton("Clear");
textRe="";
isStart=true;
s=new Socket("127.0.0.1",8000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out=new PrintWriter(s.getOutputStream());
go(); }
public void go(){
con.setLayout(new BorderLayout(2,1));
panel.setLayout(new GridLayout(2,2));
con.add(scrollPane,BorderLayout.CENTER);
con.add(panel,BorderLayout.SOUTH);
panel.add(label);
panel.add(textField);
panel.add(addButton);
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText("");
out.println(clientName+": "+text);
out.flush();
}});
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
textArea.setText("");
textField.setText("");
}});
textField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
String text=textField.getText();
textField.setText("");
out.println(clientName+": "+text);
out.flush();
}});
frame.addWindowListener(new WindowAdapter(){
public void windowActivated(WindowEvent arg0) {
textField.requestFocusInWindow();
}});
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
try {
isStart=false;
out.println(clientName +" is leaving");
out.flush();
out.println("78987");
out.flush();
s.close();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
});
panel.add(clearButton);
frame.setSize(200,300);
frame.setVisible(true);
out.println(clientName+": is coming!");
out.flush();
while(isStart){
try {
textRe = in.readLine();
} catch (Exception e) {
e.printStackTrace();
break;
}
textArea.append(textRe+"\n");
}
}
}