//file name:Login.java
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;public class Login extends JFrame implements ActionListener{

JFrame jf = new JFrame("login");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JLabel l1 = new JLabel("Input your name:");
JLabel l2 = new JLabel("Input the server IP");
JTextField t1 = new JTextField();
JTextField t2 = new JTextField("127.0.0.1");
JButton loginButton=new JButton("Login");

public Login(){
p1.setLayout(new GridLayout(2,2));
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p2.setLayout(new FlowLayout());
p2.add(loginButton);
jf.getContentPane().add(p1,"North");
jf.getContentPane().add(p2);
jf.pack();
jf.setLocation(300,200);
jf.setVisible(true);
t1.addActionListener(this);
t2.addActionListener(this);
loginButton.addActionListener(this);
}

public void actionPerformed(ActionEvent e){
if(!t1.getText().equals("")  && !t2.getText().equals(""))
{
TestClient c=new TestClient(t1.getText(),t2.getText()); //start the client's windows
jf.setVisible(false);
}
}
public static void main(String[] args){
Login aa= new Login();
}


}_________________________________________________
//file name :ReadThread.java
import java.io.DataInputStream;
import java.io.IOException;import javax.swing.JTextArea;class ReadThread extends Thread{
JTextArea ta;
DataInputStream dis;
public ReadThread(JTextArea t,DataInputStream d)
{
this.ta = t;
this.dis =d;
    }
    
    public void run(){
    
     try
     {
     while(true)
     {
     ta.append("I say: "+dis.readUTF());  //accept the msg
     ta.append("\n");
     }  
         }
         catch(IOException e)
            {
             System.out.println("conncttiong error");
            }                                  
          
    }       
}   

解决方案 »

  1.   

    // file name:TestClient.java
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;public class TestClient  extends JFrame implements ActionListener{

    DataInputStream dis;
    DataOutputStream dos;
    JTextField tf;
    JTextArea ta;
    String s11,s22;


    public TestClient(String s1,String s2){

    this.setTitle("Client");
    JScrollPane jp = new JScrollPane();

    ta = new JTextArea(10,10);
    Panel p = new Panel();
    tf = new JTextField(20);
    JButton b = new JButton("Send");
    b.addActionListener(this);
    tf.addActionListener(this);
    p.add(tf);
    p.add(b);
    jp.setViewportView(ta);
    this.getContentPane().add(jp);
    this.getContentPane().add("South",p);
    this.setSize(350,250);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.s11 = s1; //接受用户昵称信息
    this.s22 = s2; //接受服务器地址信息
    this.setVisible(true);
    tf.requestFocus();
    this.connect();
    this.createReadThread();
    }

    public void connect(){
    try{
    Socket s2 = new Socket(s22,911);
    InputStream is = s2.getInputStream();
    dis = new DataInputStream(is);
    OutputStream os = s2.getOutputStream();
    dos = new DataOutputStream(os);
    }catch(IOException e){
    System.out.println("server error!");
    }
    }

    public void createReadThread(){
    ReadThread rt = new ReadThread(this.ta,this.dis);
    rt.start();
    }

    public void actionPerformed(ActionEvent e){
    try{
    String s = tf.getText();
    dos.writeUTF(s11 + "say " + s); 
    ta.append("I say" + s);
    ta.append("\n");
    tf.setText("");
    tf.requestFocus();
       }
       catch(IOException e1)
       {
    e1.printStackTrace();
        }
    }
    }___________________________________________________________
    file name:TestServer.java
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;public class TestServer extends JFrame implements ActionListener{
    DataInputStream dis;
    DataOutputStream dos;
    JTextField tf;
    JTextArea ta;

    public TestServer(){
    this.setTitle("server");
    JScrollPane jp = new JScrollPane();

    ta = new JTextArea(10,10);
    Panel p = new Panel();
    tf = new JTextField(20);
    JButton b = new JButton("Send");
    b.addActionListener(this);
    tf.addActionListener(this);
    p.add(tf);
    p.add(b);
    jp.setViewportView(ta);
    this.getContentPane().add(jp);
    this.getContentPane().add("South",p);
    this.setSize(350,250);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    tf.requestFocus();
    this.connect(); //建立连接
    this.createReadThread();  //启动接受信息线程
        }    
        
        public void connect(){
         try
         {
         ServerSocket ss = new ServerSocket(911); //准备通信端口
             Socket s2 = ss.accept();
             InputStream is = s2.getInputStream();
             dis = new DataInputStream(is); //准备输入流
             OutputStream os = s2.getOutputStream();
             dos = new DataOutputStream(os); //准备输出流
            }
            catch(IOException e)
            {
             System.out.println("connection error");
            }             
        }
        
        public void createReadThread(){
         ReadThread rt = new ReadThread(this.ta,this.dis);
         rt.start();
        }
        
        public void actionPerformed(ActionEvent e){
         try
         {
         String s = tf.getText();
         dos.writeUTF(s); //发送聊天信息
         ta.append("I say"+s);
         ta.append("\n");
         tf.setText("");
         tf.requestFocus();    
            }  
            catch(IOException e1)
            {
             e1.printStackTrace();
            }           
        } 
        
        public static void main(String[] args){
    new TestServer();
        }
    } 改造后的功能描述:
    server随即生成一个0~100的数字,在client提交一个数字,随后由server返回一个提示输入的与server随即生成的那个数是否相等,大了小了都给予提示,直到相等。 
      

  2.   

    TestClient.java
    public void actionPerformed(ActionEvent e){
    try{
    String s = tf.getText();
    dos.writeUTF(s); 
    ta.append("The client input:" + s);
    ta.append("\n");
    tf.setText("");
    tf.requestFocus();
       }
       catch(IOException e1)
       {
    e1.printStackTrace();
        }
    //Server
     public void actionPerformed(ActionEvent e){
         try
         {
         String s = tf.getText();
         dos.writeUTF(s); //发送聊天信息
         ta.append("I say"+s);
         ta.append("\n");
         tf.setText("");
         tf.requestFocus();    
            }  
            catch(IOException e1)
            {
             e1.printStackTrace();
            }           
        } //
     public void run(){
        
         try
         {
         while(true)
         {           
                                         int i = jugeNumber(Integer.parseInt(dis.readUTF()));//产生随机数字,并判断大小,我不会写,你自己写吧
                                        if(i > 0)//比客户端的大
                                        {
                                              ta.append("The number you input is smaller");  
                                         }else {
                                         }//省略了,自己补充吧      ta.append("\n");
         }  
             }
             catch(IOException e)
                {
                 System.out.println("conncttiong error");
                }
      

  3.   

    无非就是在run里面加一个判断,不过你要把Client和Server的线程类做成两个
      

  4.   

    ta.append("The number you input is smaller");  没有提交成功啊。
      

  5.   

    不知道server如何获取client提交的数,请问这个怎么做呢?
      

  6.   

    改成这样都提交不了:try
         {
         while(true)
         {           
                                         int i = jugeNumber(Integer.parseInt(dis.readUTF()));//产生随机数字,并判断大小,我不会写,你自己写吧
                                        if(1> 0)//比客户端的大
                                        {
                                              ta.append("The number you input is smaller");  
                                         }else {
                                         }//省略了,自己补充吧      ta.append("\n");
         }  
             }
      

  7.   

    ////////-----------供你参考
    //file name :ReadThread.java
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.util.*;import javax.swing.JTextArea;class ReadThread extends Thread{
    JTextArea ta;
    int randomNumber=-1;
    DataInputStream dis;
    DataOutputStream dos;

    public ReadThread(JTextArea t,DataInputStream d,DataOutputStream dos){
    this.ta = t;
    this.dis =d;
    this.dos = dos;
    } public void run(){
    int i=0;
    String s="";
    try{
    while(true){
    s =dis.readUTF();

    if (s.endsWith("say $+$getnumber")){

    Random random = new Random();
    randomNumber = random.nextInt(100);
    dos.writeUTF("产生了一个100以内的数");
    }
    if (!s.endsWith("太大了")&&
    !s.endsWith("太小了")&&
    !s.endsWith("产生了一个100以内的数")&&
    !s.endsWith("不是一个数字")&&
    !s.endsWith("say $+$getnumber")){
    System.out.println(s);
    try{
    int a = s.indexOf("say ");
    String ss = s.substring(a+4,s.length());
    i=Integer.parseInt(ss);
    }
    catch(NumberFormatException e){
    dos.writeUTF("不是一个数字");
    }

    if (i!=randomNumber){
    if(i>randomNumber){
    dos.writeUTF(i+"太大了");
    }else{
    dos.writeUTF(i+"太小了");
    }
    }else{
    dos.writeUTF(i+"对了");
    } }
    ta.append("I say: "+s); //accept the msg
    ta.append("\n");





    }
    catch(IOException e){
    System.out.println("conncttiong error");


    public int getRandomNumber(){
    return randomNumber;
    }}
      

  8.   

    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;public class Login extends JFrame implements ActionListener{ JFrame jf = new JFrame("login");
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JLabel l1 = new JLabel("Input your name:");
    JLabel l2 = new JLabel("Input the server IP");
    JTextField t1 = new JTextField();
    JTextField t2 = new JTextField("127.0.0.1");
    JButton loginButton=new JButton("Login"); public Login(){
    p1.setLayout(new GridLayout(2,2));
    p1.add(l1);
    p1.add(t1);
    p1.add(l2);
    p1.add(t2);
    p2.setLayout(new FlowLayout());
    p2.add(loginButton);
    jf.getContentPane().add(p1,"North");
    jf.getContentPane().add(p2);
    jf.pack();
    jf.setLocation(300,200);
    jf.setVisible(true);
    t1.addActionListener(this);
    t2.addActionListener(this);
    loginButton.addActionListener(this);
    } public void actionPerformed(ActionEvent e){
    if(!t1.getText().equals("") && !t2.getText().equals("")){
    TestClient c=new TestClient(t1.getText(),t2.getText()); //start the client's windows
    c.getRandomNumber();
    jf.setVisible(false);
    }
    }
    public static void main(String[] args){
    Login aa= new Login();
    }
    }
      

  9.   

    // file name:TestClient.java
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.Socket;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;public class TestClient extends JFrame implements ActionListener{ DataInputStream dis;
    DataOutputStream dos;
    JTextField tf;
    JTextArea ta;
    String s11,s22;
    public TestClient(String s1,String s2){ this.setTitle("Client");
    JScrollPane jp = new JScrollPane(); ta = new JTextArea(10,10);
    Panel p = new Panel();
    tf = new JTextField(20);
    JButton b = new JButton("Send");
    b.addActionListener(this);
    tf.addActionListener(this);
    p.add(tf);
    p.add(b);
    jp.setViewportView(ta);
    this.getContentPane().add(jp);
    this.getContentPane().add("South",p);
    this.setSize(350,250);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.s11 = s1; //接受用户昵称信息
    this.s22 = s2; //接受服务器地址信息
    this.setVisible(true); 
    tf.requestFocus();
    this.connect();
    this.createReadThread();
    } public void connect(){
    try{
    Socket s2 = new Socket(s22,911);
    InputStream is = s2.getInputStream();
    dis = new DataInputStream(is);
    OutputStream os = s2.getOutputStream();
    dos = new DataOutputStream(os); 
    }catch(IOException e){
    System.out.println("server error!"); 
    }
    } public void createReadThread(){
    ReadThread rt = new ReadThread(this.ta,this.dis,this.dos);
    rt.start();
    } public void actionPerformed(ActionEvent e){
    try{
    String s = tf.getText();
    dos.writeUTF(s11 + "say " + s); 
    ta.append("I say" + s);
    ta.append("\n");
    tf.setText(""); 
    tf.requestFocus();
    }
    catch(IOException e1){
    e1.printStackTrace();
    }
    }
    public void getRandomNumber(){
    try{
    String s = "$+$getnumber";
    dos.writeUTF(s11 + "say " + s); 
    }
    catch(IOException e1){
    e1.printStackTrace();
    }
    }
    }
      

  10.   

    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;public class TestServer extends JFrame implements ActionListener{
    DataInputStream dis;
    DataOutputStream dos;
    JTextField tf;
    JTextArea ta; public TestServer(){ this.setTitle("server");
    JScrollPane jp = new JScrollPane(); ta = new JTextArea(10,10);
    Panel p = new Panel();
    tf = new JTextField(20);
    JButton b = new JButton("Send");
    b.addActionListener(this);
    tf.addActionListener(this);
    p.add(tf);
    p.add(b);
    jp.setViewportView(ta);
    this.getContentPane().add(jp);
    this.getContentPane().add("South",p);
    this.setSize(350,250);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true); 
    tf.requestFocus();
    this.connect(); //建立连接
    this.createReadThread(); //启动接受信息线程

    }  public void connect(){
    try{
    ServerSocket ss = new ServerSocket(911); //准备通信端口
    Socket s2 = ss.accept();
    InputStream is = s2.getInputStream();
    dis = new DataInputStream(is); //准备输入流
    OutputStream os = s2.getOutputStream();
    dos = new DataOutputStream(os); //准备输出流
    }
    catch(IOException e){
    System.out.println("connection error"); 

    } public void createReadThread(){
    ReadThread rt = new ReadThread(this.ta,this.dis,this.dos);
    rt.start();
    }  public void actionPerformed(ActionEvent e){
    try{
    String s = tf.getText();
    dos.writeUTF(s); //发送聊天信息
    ta.append("I say"+s);
    ta.append("\n");
    tf.setText("");
    tf.requestFocus(); 

    catch(IOException e1){
    e1.printStackTrace();

    }  public static void main(String[] args){
    new TestServer(); 

    }
    -----------------------
    肯定不是最好的办法。
    但是有那么点意思。
    楼主可以参考一下,自己写一个。
      

  11.   

    to:
    john_sheep(彩虹勇士(楼主,没事就把贴子结了吧.)) 
    难得兄弟这么谦虚又多才,非常谢谢啊!
    再送你200分。