////////////////Frame1.java
package aq;import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.net.*;
import java.io.*;
import java.lang.String;/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author popco2
 * @version 1.0
 */public class Frame1 extends JFrame implements Runnable {
  JPanel contentPane;
  Panel panel1 = new Panel();
  TextField textField1 = new TextField();
  Button send = new Button();
  Button quit = new Button();
  Panel panel2 = new Panel();
  Label label1 = new Label();
  TextField name = new TextField();
  Label label2 = new Label();
  Button link = new Button();
  Button relink = new Button();
  Button save = new Button();
  Socket socket = null;
  BufferedReader sin = null;
  PrintWriter sout = null;
  TextArea textArea1 = new TextArea();
  JPasswordField password = new JPasswordField();
  MenuBar menuBar1 = new MenuBar();
  Menu menu1 = new Menu();
  MenuItem menuItem1 = new MenuItem();
  MenuItem menuItem2 = new MenuItem();
  MenuItem menuItem3 = new MenuItem();
  MenuItem menuItem4 = new MenuItem();
  Menu menu2 = new Menu();
  MenuItem menuItem5 = new MenuItem();
  MenuItem menuItem6 = new MenuItem();
  Menu menu3 = new Menu();
  MenuItem menuItem7 = new MenuItem();
  MenuItem menuItem8 = new MenuItem();
  Button open = new Button();
  XYLayout xYLayout1 = new XYLayout();
  Label label3 = new Label();
  Label label4 = new Label();
  TextField hostAddr = new TextField();
  TextField port = new TextField();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  GridBagLayout gridBagLayout2 = new GridBagLayout();  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  //Component initialization
  private void jbInit() throws Exception  {
    //setIconImage(Toolkit.getDefaultToolkit().createImage(Frame1.class.getResource("[Your Icon]")));
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(xYLayout1);
    this.setSize(new Dimension(469, 251));
    this.setTitle("答疑程式——客户端");
    textField1.setColumns(26);
    send.setLabel("发送");
    send.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        send_actionPerformed(e);
      }
    });
    quit.setLabel("退出");
    quit.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        quit_actionPerformed(e);
      }
    });
    label1.setText("学生");
    name.setColumns(8);
    label2.setText("口令");
    link.setActionCommand("登录");
    link.setLabel("登录");
    link.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        link_actionPerformed(e);
      }
    });
    relink.setLabel("注销");
    save.setLabel("保存");
    save.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        save_actionPerformed(e);
      }
    });
    password.setColumns(8);
    menu1.setLabel("动作");
    menuItem1.setLabel("发送");
    menuItem2.setLabel("保存");
    menuItem3.setLabel("打开");
    menuItem4.setLabel("退出");
    menu2.setLabel("状态");
    menuItem5.setLabel("登录");
    menuItem6.setLabel("注销");
    menu3.setLabel("帮助");
    menuItem7.setLabel("动画帮助");
    menuItem8.setLabel("关于AQ");
    textArea1.setEditable(false);
    open.setLabel("打开");
    open.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        open_actionPerformed(e);
      }
    });
    label3.setText("AQ服务器地址");
    label4.setText("端口");
    hostAddr.setColumns(4);
    port.setColumns(15);
    panel1.setLayout(gridBagLayout2);
    panel2.setLayout(gridBagLayout1);
    contentPane.setBackground(new Color(212, 208, 200));
    panel1.add(textField1,                new GridBagConstraints(0, 0, 4, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 5, 0, 8), 362, 0));
    panel1.add(quit,        new GridBagConstraints(4, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 3, 12), 0, 0));
    panel1.add(label3,        new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 17, 9, 0), 0, 0));
    panel1.add(port,            new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 0, 3, 0), 165, 0));
    panel1.add(label4,     new GridBagConstraints(2, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(11, 8, 3, 0), 0, 0));
    panel1.add(hostAddr, new GridBagConstraints(3, 1, 1, 1, 1.0, 0.0
            ,GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(6, 0, 6, 10), 47, 0));
    panel1.add(send, new GridBagConstraints(4, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(3, 1, 0, 17), 0, 0));
    panel2.add(label1,  new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 9, 0, 0), 0, 0));
    panel2.add(name,  new GridBagConstraints(1, 0, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 0, 0, 9), 0, 0));
    panel2.add(label2,  new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(9, 9, 0, 0), 0, 0));
    panel2.add(password,  new GridBagConstraints(1, 1, 2, 1, 1.0, 0.0
            ,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(7, 0, 0, 9), 0, 0));
    panel2.add(link,  new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(7, 9, 0, 0), 0, 0));
    panel2.add(relink,  new GridBagConstraints(2, 2, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(7, 0, 0, 9), 0, 0));
    panel2.add(save,  new GridBagConstraints(0, 3, 2, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 9, 12, 0), 0, 0));
    panel2.add(open,  new GridBagConstraints(2, 3, 1, 1, 0.0, 0.0
            ,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 12, 9), 0, 0));
    contentPane.add(panel1,       new XYConstraints(3, 135, 470, 66));
    contentPane.add(textArea1,      new XYConstraints(0, 0, 332, 133));
    contentPane.add(panel2,  new XYConstraints(330, 4, 141, 134));

解决方案 »

  1.   

    //接上    menuBar1.add(menu1);
        menuBar1.add(menu2);
        menuBar1.add(menu3);
        menu1.add(menuItem1);
        menu1.add(menuItem2);
        menu1.add(menuItem3);
        menu1.addSeparator();
        menu1.add(menuItem4);
        menu2.add(menuItem5);
        menu2.add(menuItem6);
        menu3.add(menuItem7);
        menu3.add(menuItem8);
        setMenuBar(menuBar1);
      }
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          quit();
        }
      }  void link_actionPerformed(ActionEvent e) {
        Thread thread = new Thread(this);
        thread.start();
      }  void send_actionPerformed(ActionEvent e) {
        sout.println(textField1.getText());
        sout.flush();
        textArea1.append("客户端消息:"+textField1.getText()+"\n");
        textField1.setText("");
      }  void quit_actionPerformed(ActionEvent e) {
        quit();
      }
      public void run() {
        /**@todo: Implement this java.lang.Runnable method*/
        try{
            socket = new Socket("LocalHost",1124);//这两个参数可以从用户定义的地方获取
            sin = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            sout = new PrintWriter(socket.getOutputStream());
            send.setEnabled(true);
            Receiver r = new Receiver();
            Thread t = new Thread(r);
            t.start();
            textArea1.append("恭喜!成功连接到老师的服务器!\n");
            link.setEnabled(false);
        }catch(Exception e){
          textArea1.append(e.toString()+"\n");
        }
      }  private class Receiver implements Runnable{
        public void run(){
          String msg = null;
          try{
            msg = sin.readLine();
            while(msg!="ServerExits!"){
              textArea1.append("服务器消息:"+msg+"\n");
              msg = sin.readLine();
            }
            sin.close();
            sout.close();
            socket.close();
          }catch(Exception e){}
          send.setEnabled(false);    //服务器不在服务中,禁止消息发送
        }
      }  void quit(){
        try{
            sout.println("客户端退出!");
            sout.flush();
        }catch(Exception e){}
        finally{
          System.exit(0);
        }
      }  void save_actionPerformed(ActionEvent e) {
          try{
              FileOutputStream file = new FileOutputStream("note.txt");
              BufferedOutputStream buff = new BufferedOutputStream(file);
              buff.flush();//////flush  有问题???????????
              buff.close();
              textArea1.append("AQ保存成功!\n");
          }catch(IOException ex){
              textArea1.append(ex.toString()+"\n");
          }
      }  void open_actionPerformed(ActionEvent e) {
          try{
              FileInputStream file = new FileInputStream("note.txt");
              BufferedInputStream buff = new BufferedInputStream(file);
              if(buff.available()!=-1){////当  Note.txt  为空时 available?
                textArea1.append(buff.read()+"\nAQ读入完成!\n");
                buff.close();
                }
              else{
                textArea1.append("AQ暂时无记录\n");
                }
          }catch(IOException ex){
              textArea1.append(ex.toString()+"\n");
          }
      }}
      

  2.   

    server端开FTP了么??干嘛非要FTP功能??
      

  3.   

    其实我的client只是一个Demo,server端不存在
    想在线交流时免不了要交换文件,就需要Ftp了,我知道加个Ftp功能是比较麻烦
    但努力做吧,争取能实现
      

  4.   

    你真是吃饱了撑的,呵呵。要交换文件直接用socket的InputStream/OutputStream就行了。用FTP干嘛?还要研究FTP协议!!!!传输文件不是都用FTP的!!!!
      

  5.   

    是有点吃撑了,胃口好大:)我想要个像QQ里的传送文件那种方式,有文件选择。要是socket的InputStream/OutputStream具体怎么实现,给个Demo
    如能实现,高分回报
      

  6.   

    你要先有一个自定义的通讯协议,比如A要发文件给B,则在现有的连接中发一个指令,比如:SendFile from BA接到后,就准备好接收:ServerSocket ss = new ServerSocket(自定义一个专用文件接收的端口);
    Socket socket = ss.accept();
    BufferedInputStream remoteFile = new BufferedInputStream(socket.getInputStream());
    BufferedOutputStream saveFile = new BufferedOutputStream(new FileOutputStream(指定存放文件的路径和文件名));byte[] buffer = new byte[1024*128];
    int b;
    while ((b=remoteFile.read(buffer)) != -1)
    {
       saveFile.write(buffer, 0, b);
    }remoteFile.close();
    saveFile.close();
    socket.close();
    ss.close();
    在B端发文件,则要:Socket socket = new Socket(对方IP地址,文件传输端口);
    BufferedInputStream localFile = new BufferedInputStream(new FileInputStream(文件路径和文件名));
    BufferedOutputStream sendFile = new BufferedOutputStream(socket.getOutputStream());byte[] buffer = new byte[1024*128];
    int b;
    while ((b=localFile.read(buffer)) != -1)
    {
       sendFile.write(buffer, 0, b);
    }localFile.close();
    sendFile.close();
    socket.close();
      

  7.   

    在我的application中A端怎么打开收到的文件?
    1。文件格式已知
    2。文件格式未知
      

  8.   

    格式无需知道,按二进制的InputStream/OutputStream操作就没问题。
      

  9.   

    哦,明白了,谢谢我这里还有个帖子是关于这个application的:
    就是如何向Server端申请身份验证
    想要个Demo,望得到帮助,详见:
    http://www.csdn.net/expert/topic/731/731909.xml?temp=.6492426我给出的代码中有:
    void save_actionPerformed(ActionEvent e) {
          try{
              FileOutputStream file = new FileOutputStream("note.txt");
              BufferedOutputStream buff = new BufferedOutputStream(file);
              buff.flush();//////flush  有问题???????????
              buff.close();
              textArea1.append("AQ保存成功!\n");
          }catch(IOException ex){
              textArea1.append(ex.toString()+"\n");
          }
      }  void open_actionPerformed(ActionEvent e) {
          try{
              FileInputStream file = new FileInputStream("note.txt");
              BufferedInputStream buff = new BufferedInputStream(file);
              if(buff.available()!=-1){////当  Note.txt  为空时 应该怎么判断?
                textArea1.append(buff.read()+"\nAQ读入完成!\n");
                buff.close();
                }
              else{
                textArea1.append("AQ暂时无记录\n");
                }
          }catch(IOException ex){
              textArea1.append(ex.toString()+"\n");
          }
      }/////////////////////////////////
    他们是用来实现对textArea1中的聊天记录进行保存,打开的,麻烦看看有没有错
    能不能实现保存,打开?望给予修正!Thanx!
      

  10.   

    上面:你还没写东西呢,干嘛flush()?buff.available() == 0 就说明为空呗一般都是if (buff.available() > 0)  do....
      

  11.   

    老弟,你就做了个界面呀???
       你里面好多东东都还有问题呢!(用什么awt组件呀!),什么方法就大概写了个框架,你还不如直接要别人现成的程序算了!