这段代码,我是先定义了接收端的路径,然后发送到指定的地方,我试过加多一个按钮的功能能直接从路径中把已经发送到的图片显示出来,可是这样很不方便,请各位高手教一教在下,怎样可以接受端的一方一接受到对方的图片保存到指定路径后,直接显示出来,不需要另外一个按钮做跳板!而且这段代码只能发送一个图片,因为还没交上循环代码。还不能刷出一张新的图片。
以下是代码,希望各位高手帮忙修改一下,在下已经苦恼了3到4个星期了,而且快要到交作业的期限了,请求各位高手帮帮忙

解决方案 »

  1.   

    import java.awt.EventQueue;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.awt.image.ImageObserver;
    import java.awt.image.ImageProducer;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketAddress;
    import java.util.GregorianCalendar;
    import java.util.Vector;
    import javax.swing.BorderFactory;
    import javax.swing.*;
    import org.omg.CORBA.portable.InputStream;
    import org.omg.CORBA.portable.OutputStream;
    import java.awt.*;
    import javax.swing.JPanel;
    public class QQ<FontAttrib> extends JFrame { JFrame frame;
    private JTextPane textPane_1 ;  
     private StyledDocument doc; // 非常重要插入文字样式就靠它了
     private StyledDocument dic ; // 非常重要插入文字样式就靠它了
       JTextPane textPane = new JTextPane();  
           Toolkit tk;
           Vector picVector = new Vector();
           private JTextField textField;
           int length=1024;
       byte[] sendBytes=new byte[1024];
       GregorianCalendar time=new GregorianCalendar();
       ByteArrayOutputStream baos = new ByteArrayOutputStream();  
     int a1=0;
     String sj=new String(""+time.get(GregorianCalendar.YEAR)+
            +time.get(GregorianCalendar.MONTH)+
            +time.get(GregorianCalendar.DATE)+
            +time.get(GregorianCalendar.HOUR)+
            +time.get(GregorianCalendar.MINUTE)+
            +time.get(GregorianCalendar.SECOND));
     Image image;
     Graphics g;
     Thread th=new Thread();
      JTextField textField_1;
     
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {

    try {
    QQ window = new QQ();
    window.frame.setVisible(true);

    } catch (Exception e) {
    e.printStackTrace();
    }

    }
    });
    } /**
     * Create the application.
     */
    public QQ() {

    initialize();
    } /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 625, 550);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    scrollPane_1 = new JScrollPane(textPane_1);

    textField_1 = new JTextField();
    textField_1.setBounds(246, 485, 195, 21);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);
    textField_1.setText("f:\\asd.jpg");

    textPane.setBounds(0, 0, 442, 272);
    frame.getContentPane().add(textPane);
     dic = textPane.getStyledDocument();

    final JTextPane textPane_1 = new JTextPane();
    textPane_1.setBounds(0, 301, 442, 146);
    frame.getContentPane().add(textPane_1);
     doc = textPane_1.getStyledDocument();
     
     textField = new JTextField();
    textField.setBounds(10, 485, 212, 21);
    frame.getContentPane().add(textField);
    textField.setColumns(10);
     
    JLabel label = new JLabel("\u6CA1\u6709\u56FE\u7247");
    label.setBounds(285, 276, 156, 15);
    frame.getContentPane().add(label);

    JButton button = new JButton("\u56FE\u7247");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     JFileChooser f = new JFileChooser(); // 查找文件
      f.showOpenDialog(null);
      insertIcon(f.getSelectedFile()); 
      textPane_1.insertIcon(new ImageIcon(f.getSelectedFile().toString()));
     textField.setText(f.getSelectedFile()+"");

    } private void insertIcon(File file) {
    // TODO Auto-generated method stub

     textPane_1.setCaretPosition(doc.getLength()); // 设置插入位置
     textPane_1.insertIcon(new ImageIcon(file.getPath())); // 插入图片

     
    }
    });




    JButton button_1 = new JButton("\u6E05\u7A7A");
    button_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    textPane_1.setText("");
    }
    });
    button_1.setBounds(246, 457, 93, 23);
    frame.getContentPane().add(button_1);

    JButton button_2 = new JButton("\u53D1\u9001");
     button_2.addActionListener(new ActionListener()  
      { // 插入文字的事件
      public void actionPerformed(ActionEvent e)  
      {
     
       //       paint();
            
      } private void insert(Object fontAttrib) {
    // TODO Auto-generated method stub

    }
      });//
    button_2.setBounds(349, 457, 93, 23);
    frame.getContentPane().add(button_2);



    List list = new List();
    list.setBounds(445, 0, 172, 516);
    frame.getContentPane().add(list);
    waitfordata();
    JButton button_3 = new JButton("\u53D1\u9001\u56FE\u7247");
    button_3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sendatatupian();


    }

    private void sendatatupian() {
    // TODO Auto-generated method stub

      Socket s=null;
            ObjectOutputStream oos=null;
            try {
                s=new Socket("127.0.0.1",8888);
                oos=new ObjectOutputStream(s.getOutputStream());
                oos.writeObject(new File(textField.getText()));
                oos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                if(oos!=null)
                    try {
                        oos.close();
                    } catch (IOException e1) {
                    } 
               
           
                if(s!=null)try {s.close();} catch (IOException e) {}         }
    //        copyContent();

    } private void copyContent() { // TODO Auto-generated method stub



    Vector<String> picVector = new Vector<String>();
    int loc = textPane.getDocument().getLength();
    // System.out.println(loc);
    for (int i = 0; i < textPane_1.getStyledDocument().getRootElements()[0]
    .getElement(0).getElementCount(); i++) {
    Icon icon = StyleConstants.getIcon(textPane_1.getStyledDocument()
    .getRootElements()[0].getElement(0).getElement(i)
    .getAttributes());
    if (icon != null) {
    picVector.add(icon.toString());
    }
    }
    int k = 0;
    for (int i = 0; i < textPane_1.getText().length(); i++) {
    if (textPane_1.getStyledDocument().getCharacterElement(i).getName()
    .equals("icon")) {
    textPane.insertIcon(new ImageIcon(picVector.get(k).toString()));
    picVector.get(k++);
    } else {
    try {
    textPane.getStyledDocument().insertString(textPane.getText().length(),
    textPane_1.getStyledDocument().getText(i, 1), null);
    } catch (BadLocationException ble) {
    ble.printStackTrace();
    }
    }
    }
    picVector.clear(); }
    });


     
     
     
     

    button_3.setBounds(129, 457, 93, 23);
    frame.getContentPane().add(button_3);






    }
     private void insert(FontAttrib attrib) {

     }

    protected Object getFontAttrib() {
    // TODO Auto-generated method stub
    return null;

    }

    public  void waitfordata()  {

    th.start();
            ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;
            
            
            try {
                ss=new ServerSocket(8888);
               
                while (true) {
                    s = ss.accept();
                    
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(textField_1.getText()));
                   
                    fis=new FileInputStream(a);
                  
                    while(fis.available()>0){
                        fos.write(fis.read());
                        
                    }
                                   if(fis.available()<0){
                    label.setText("可以显示图片了");
                    }
                  
                  
                
            
                }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
                if(fos!=null)try {fos.close();} catch (IOException e1) {}
                if(fis!=null)try{fis.close();}catch (IOException e) {}
                if(s!=null)try {s.close();} catch (IOException e) {}
                if(ss!=null)try {ss.close();} catch (IOException e) {}
                
            
                    
        }
      
    public void paint() {
    // TODO Auto-generated method stub
    textPane.setCaretPosition(dic.getLength()); // 设置插入位置
     textPane.insertIcon(new ImageIcon(textField_1.getText())); // 插入图片
    } private void insertIcon1() {

    // TODO Auto-generated method stub
            ImageIcon imageIcon=new ImageIcon(textField_1.getText());
            image=imageIcon.getImage();       g.drawImage(image, 0, 0, this);
       textPane.paint(g);
            
    } private ImageIcon createImageIcon(String string) {
    // TODO Auto-generated method stub
    return null;
    }
    }
      

  2.   

    我调用了,下面是接收端的代码,可是好像不太管用,我把调用图片的方法的语句放在接收玩图片之后,可是运行的时候,不知道为什么要按两次运行才能运行成功,而且好像发送不太成功
    textField_1 = new JTextField();
    textField_1.setBounds(246, 485, 195, 21);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);
    textField_1.setText("f:\\asd.jpg");//用来存放路径的textField,方便接收端使用public void waitfordata() {//接收端th.start();
    ServerSocket ss=null;
    Socket s=null;
    FileOutputStream fos=null;
    ObjectInputStream ois=null;
    FileInputStream fis=null;
    try {
    ss=new ServerSocket(8888);
    while (true) {
    s = ss.accept();
    ois = new ObjectInputStream(s.getInputStream());
    File a=(File)ois.readObject();
    fos = new FileOutputStream(new File(textField_1.getText()));
    fis=new FileInputStream(a);
    while(fis.available()>0){
    fos.write(fis.read());
      }
    paint()//在这里调用显示图片
    }
      
    }
    catch (Exception e) {
      
    e.printStackTrace();
    }
    if(fos!=null)try {fos.close();} catch (IOException e1) {}
    if(fis!=null)try{fis.close();}catch (IOException e) {}
    if(s!=null)try {s.close();} catch (IOException e) {}
    if(ss!=null)try {ss.close();} catch (IOException e) {}
      
      
      }
    public void paint() {//用来接收完图片后显示图片
    // TODO Auto-generated method stub
    textPane.setCaretPosition(dic.getLength()); // 设置插入位置
    textPane.insertIcon(new ImageIcon(textField_1.getText())); // 插入图片
    }
      

  3.   

    还是老问题,你还是要把每一个环节先做好再把各个环节连接起来调用显示图片的地方是不是能确保图片已经保存在本地?还有那个方法最好不要起paint的名字(可能不会引起冲突,但还是不要好点)
      

  4.   

    谢谢你来帮忙,跟住你之前的教导,我已经重新把路径获取的方法改了一遍,设置文本框里面的内容信息为接收的路径。
    public  void waitfordata()  {         ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;
            try {
                ss=new ServerSocket(8888);
                while (true) {
                    s = ss.accept();
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(textField_1.getText()));
                  
                    fis=new FileInputStream(a);
                    while(fis.available()>0){
                        fos.write(fis.read());
                    }
                                  textPane.insertIcon(new ImageIcon(textField_1.getText().toString()));//如果不利用piant()来调用,就直接把语句写在这里了,我觉得插入图片语句应该是放在这里的,如果放错了,请各位高手提醒
           textPane.setCaretPosition(dic.getLength());                           }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
                if(fos!=null)try {fos.close();} catch (IOException e1) {}
                if(fis!=null)try{fis.close();}catch (IOException e) {}
                if(s!=null)try {s.close();} catch (IOException e) {}
                if(ss!=null)try {ss.close();} catch (IOException e) {}
                      
        }
    我之前也通过多加的一个接收的按钮,接收按钮里面调用了调用本地图片显示的方法,路径也是获取文本框的内容信息为路径的,最终也可以正常显示图片。按以上的写法就要连按两次运行才能弹出窗口出来,如果各位不明白我说的是什么情况的话,可以运行上面的代码试试!
    可是现在我想不通过按钮这个跳板来显示图片,却不能正常显示,每次启动程序,都要按两次运行,才能弹出聊天窗口出来。
    其实这里,还有一个问题没解决的就是如何通过循环来刷新出每一张图片,而不会每次都覆盖原先的图片。在这里我打算先能在正常运行的情况下能显示一张图片之后再去探究循环刷出新图片的问题在这里请教各位高手了
      

  5.   


    import java.awt.EventQueue;import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.image.BufferedImage;
    import java.awt.image.ImageObserver;
    import java.awt.image.ImageProducer;
    import java.io.ByteArrayOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.InetAddress;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.SocketAddress;
    import java.util.GregorianCalendar;
    import java.util.Vector;import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextField;
    import javax.swing.JTextPane;
    import javax.swing.UIManager;
    import javax.swing.text.BadLocationException;
    import javax.swing.text.SimpleAttributeSet;
    import javax.swing.text.StyleConstants;
    import javax.swing.text.StyledDocument;import org.omg.CORBA.portable.InputStream;
    import org.omg.CORBA.portable.OutputStream;import java.awt.*;
    import javax.swing.JPanel;
    public class waitfordata<FontAttrib> extends JFrame { JFrame frame;
    private JTextPane textPane_1 ;  
     private StyledDocument doc; // 非常重要插入文字样式就靠它了
     private StyledDocument dic ; // 非常重要插入文字样式就靠它了
       JTextPane textPane = new JTextPane();  
           Toolkit tk;
           Vector picVector = new Vector();
           private JTextField textField;
           int length=1024;
       byte[] sendBytes=new byte[1024];
       GregorianCalendar time=new GregorianCalendar();
       ByteArrayOutputStream baos = new ByteArrayOutputStream();  
     int a1=0;
     String sj=new String(""+time.get(GregorianCalendar.YEAR)+
            +time.get(GregorianCalendar.MONTH)+
            +time.get(GregorianCalendar.DATE)+
            +time.get(GregorianCalendar.HOUR)+
            +time.get(GregorianCalendar.MINUTE)+
            +time.get(GregorianCalendar.SECOND));
      JTextField textField_1;
     
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {

    try {
    waitfordata window = new waitfordata();
    window.frame.setVisible(true);

    } catch (Exception e) {
    e.printStackTrace();
    }

    }
    });
    } /**
     * Create the application.
     */
    public waitfordata() {

    initialize();
    } /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 625, 550);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);
    scrollPane_1 = new JScrollPane(textPane_1);

    textField_1 = new JTextField();
    textField_1.setBounds(246, 485, 195, 21);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);
    textField_1.setText("f:\\asd.jpg");

    textPane.setBounds(0, 0, 442, 272);
    frame.getContentPane().add(textPane);
     dic = textPane.getStyledDocument();

    final JTextPane textPane_1 = new JTextPane();
    textPane_1.setBounds(0, 301, 442, 146);
    frame.getContentPane().add(textPane_1);
     doc = textPane_1.getStyledDocument();
     
     textField = new JTextField();
    textField.setBounds(10, 485, 212, 21);
    frame.getContentPane().add(textField);
    textField.setColumns(10);
     

    JButton button = new JButton("\u56FE\u7247");
    button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
     JFileChooser f = new JFileChooser(); // 查找文件
      f.showOpenDialog(null);
      insertIcon(f.getSelectedFile()); 
      textPane_1.insertIcon(new ImageIcon(f.getSelectedFile().toString()));
     textField.setText(f.getSelectedFile()+"");

    } private void insertIcon(File file) {
    // TODO Auto-generated method stub

     textPane_1.setCaretPosition(doc.getLength()); // 设置插入位置
     textPane_1.insertIcon(new ImageIcon(file.getPath())); // 插入图片

     
    }
    });




    JButton button_1 = new JButton("\u6E05\u7A7A");
    button_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    textPane_1.setText("");
    }
    });
    button_1.setBounds(246, 457, 93, 23);
    frame.getContentPane().add(button_1);

    JButton button_2 = new JButton("\u53D1\u9001");
     button_2.addActionListener(new ActionListener()  
      { // 插入文字的事件
      public void actionPerformed(ActionEvent e)  
      {
     
              paint();
            
      } private void insert(Object fontAttrib) {
    // TODO Auto-generated method stub

    }
      });//
    button_2.setBounds(349, 457, 93, 23);
    frame.getContentPane().add(button_2);



    List list = new List();
    list.setBounds(445, 0, 172, 516);
    frame.getContentPane().add(list);


    waitfordata1();

     

    JButton button_3 = new JButton("\u53D1\u9001\u56FE\u7247");
    button_3.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    sendatatupian();


    }

    private void sendatatupian() {
    // TODO Auto-generated method stub

      Socket s=null;
            ObjectOutputStream oos=null;
            try {
                s=new Socket("127.0.0.1",8888);
                oos=new ObjectOutputStream(s.getOutputStream());
                oos.writeObject(new File(textField.getText()));
                oos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally{
                if(oos!=null)
                    try {
                        oos.close();
                    } catch (IOException e1) {
                    } 
               
           
                if(s!=null)try {s.close();} catch (IOException e) {}
            }
    //        copyContent();


    } private void copyContent() { // TODO Auto-generated method stub



    Vector<String> picVector = new Vector<String>();
    int loc = textPane.getDocument().getLength();
    // System.out.println(loc);
    for (int i = 0; i < textPane_1.getStyledDocument().getRootElements()[0]
    .getElement(0).getElementCount(); i++) {
    Icon icon = StyleConstants.getIcon(textPane_1.getStyledDocument()
    .getRootElements()[0].getElement(0).getElement(i)
    .getAttributes());
    if (icon != null) {
    picVector.add(icon.toString());
    }
    }
    int k = 0;
    for (int i = 0; i < textPane_1.getText().length(); i++) {
    if (textPane_1.getStyledDocument().getCharacterElement(i).getName()
    .equals("icon")) {
    textPane.insertIcon(new ImageIcon(picVector.get(k).toString()));
    picVector.get(k++);
    } else {
    try {
    textPane.getStyledDocument().insertString(textPane.getText().length(),
    textPane_1.getStyledDocument().getText(i, 1), null);
    } catch (BadLocationException ble) {
    ble.printStackTrace();
    }
    }
    }
    picVector.clear(); }
    });


     
     
     
     

    button_3.setBounds(129, 457, 93, 23);
    frame.getContentPane().add(button_3);


    }
     private void insert(FontAttrib attrib) {

     }

    protected Object getFontAttrib() {
    // TODO Auto-generated method stub
    return null;

    }

    public  void waitfordata1()  {

    th.start();
            ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;
            
            
            try {
                ss=new ServerSocket(8888);
               
                while (true) {
                    s = ss.accept();
                    
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(textField_1.getText()));
                   
                    fis=new FileInputStream(a);
                  
                    while(fis.available()>0){
                        fos.write(fis.read());
                        
                    }
                  
               
                                 
                            }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
                if(fos!=null)try {fos.close();} catch (IOException e1) {}
                if(fis!=null)try{fis.close();}catch (IOException e) {}
                if(s!=null)try {s.close();} catch (IOException e) {}
                if(ss!=null)try {ss.close();} catch (IOException e) {}
                
            
           
            
        }
      
    public void paint() {
    // TODO Auto-generated method stub
    textPane.setCaretPosition(dic.getLength()); // 设置插入位置
     textPane.insertIcon(new ImageIcon(textField_1.getText())); // 插入图片
    }
    }
      

  6.   

    两次才显示界面是因为在waitfordata1方法中的s = ss.accept();阻断了UI后续代码的执行
    而第二次因为端口占用的问题而未得到执行,因此界面得以显示了,但也可能因此造成了楼主所说的问题
    试着将此部分代码放到新线程中执行吧
      

  7.   

    高手,您好,以waitfordata为Class名的那段,可以正常的运行,能一次运行,可是,如果不通过按钮跳板来显示图片的话,就是把服务器代码改成这样的话,就会出现两次才显示界面的情况,,还有请问高手,我对线程这方面只学了一些很基础的代码应用得不是很好,所以想请问下如果要用高手所说的新线程来解决的话,
    请给些具体的指示
    public void waitfordata() {ServerSocket ss=null;
    Socket s=null;
    FileOutputStream fos=null;
    ObjectInputStream ois=null;
    FileInputStream fis=null;
    try {
    ss=new ServerSocket(8888);
    while (true) {
    s = ss.accept();
    ois = new ObjectInputStream(s.getInputStream());
    File a=(File)ois.readObject();
    fos = new FileOutputStream(new File(textField_1.getText()));
      
    fis=new FileInputStream(a);
    while(fis.available()>0){
    fos.write(fis.read());
    }
    textPane.insertIcon(new ImageIcon(textField_1.getText().toString()));//如果不利用piant()来调用,就直接把语句写在这里了,我觉得插入图片语句应该是放在这里的,如果放错了,请各位高手提醒
    textPane.setCaretPosition(dic.getLength()); }
      
    }
    catch (Exception e) {
      
    e.printStackTrace();
    }
    if(fos!=null)try {fos.close();} catch (IOException e1) {}
    if(fis!=null)try{fis.close();}catch (IOException e) {}
    if(s!=null)try {s.close();} catch (IOException e) {}
    if(ss!=null)try {ss.close();} catch (IOException e) {}}
      

  8.   

    我根据高手您所说的去试了一下用线程中的run方法去做,的确解决了之前两次运行才能启动的问题,谢谢您
    ,可是现在还是显示不到图片,在这里为我之前的错误判断而道歉,但是现在用了run()方法之后,我在接收完图片的后面加了两句显示图片的代码
    textPane.insertIcon(new ImageIcon(textField_1.getText()));
    textPane.setCaretPosition(dic.getLength());
    然后,一运行弹出窗口就卡死了请指教
    以下是run方法的语句
     public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {

    try {
    QQ window = new QQ();
    window.frame.setVisible(true);

    } catch (Exception e) {
    e.printStackTrace();
    }
            ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;
            
            
            try {
                ss=new ServerSocket(8888);
               
                while (true) {
                    s = ss.accept();
                    
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(textField_1.getText()));
                  
                    fis=new FileInputStream(a);
                   
                    while(fis.available()>0){
                        fos.write(fis.read());
                       
                    }
                   
     textPane.insertIcon(new ImageIcon(textField_1.getText()));
    textPane.setCaretPosition(dic.getLength());
    // 这两句是为了能在接收完图片之后能显示图片的语句虽然还不能实现,
    //但是现在用了run()方法之后一运行就直接
     //卡死了
        
    //                
                   
                  
                  
                  
                  //textPane.createImage((ImageProducer) new File("f:\\ds"+a1+sj+".jpg"));
                     // textPane.insertIcon( (Icon) new File("f:\\ds"+a1+sj+".jpg"));
                 // textPane.createImage( (ImageProducer) tk.getImage("f:\\ds"+a1+sj+".jpg"));
       
               // tk.getImage("f:\\ds"+a1+sj+".jpg");
                   
                }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
                if(fos!=null)try {fos.close();} catch (IOException e1) {}
                if(fis!=null)try{fis.close();}catch (IOException e) {}
                if(s!=null)try {s.close();} catch (IOException e) {}
                if(ss!=null)try {ss.close();} catch (IOException e) {}
    //             if(textPane.getText().equals("已发送图片")){
    //             paint();
    //             }
           
    //      
    //               ImageIcon imageIcon=new ImageIcon(textField_1.getText());
    //               image=imageIcon.getImage();
    //       
    //              g.drawImage(image, 0, 0, this);
    //          textPane.paint(g);
    //               
            
        }

    });
    }
      

  9.   

    稍微看了下你的代码,你对多线程完全不熟悉是么?你需要把网络监听部分的代码,用单独线程来实现,而不是直接占用GUI线程,这样会造成整个界面失去响应。简单点来说,你需要把 waitfordata() 这个函数,单独用类来实现。
    class ThreadForData extends Thread {
      public ThreadForData(...) {
        // 构造函数,可以用来传递你所需的某些参数,比如窗体对象,然后将其保存至成员变量中
      }
      public void run() { // 这个函数的定义和参数绝对不能修改
        // 原waitfordata()的代码。
      }
    }启动时可以: new ThreadForData(...).start(); 
      

  10.   

    请问我按你的要求改成这样,合适吗
    class ThreadForData extends Thread {
      public ThreadForData() {
      Thread th=new Thread();  // 构造函数,可以用来传递你所需的某些参数,比如窗体对象,然后将其保存至成员变量中
      }
      public void run() { 
      ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;// 这个函数的定义和参数绝对不能修改
      try {
                ss=new ServerSocket(8888);
               
                while (true) {
                    s = ss.accept();
                    
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(textField_1.getText()));
                  
                    fis=new FileInputStream(a);
                   
                    while(fis.available()>0){
                        fos.write(fis.read());
                       
                    }
                   
                  textPane.insertIcon(new ImageIcon(textField_1.getText()));
           textPane.setCaretPosition(dic.getLength());
           // 这两句是为了能在接收完图片之后能显示图片的语句虽然还不能实现,
           //但是现在用了run()方法之后一运行就直接
           //卡死了
        
    //                
                   
                  
                  
                  
                  //textPane.createImage((ImageProducer) new File("f:\\ds"+a1+sj+".jpg"));
                     // textPane.insertIcon( (Icon) new File("f:\\ds"+a1+sj+".jpg"));
                 // textPane.createImage( (ImageProducer) tk.getImage("f:\\ds"+a1+sj+".jpg"));
       
               // tk.getImage("f:\\ds"+a1+sj+".jpg");
                   
                }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
                if(fos!=null)try {fos.close();} catch (IOException e1) {}
                if(fis!=null)try{fis.close();}catch (IOException e) {}
                if(s!=null)try {s.close();} catch (IOException e) {}
                if(ss!=null)try {ss.close();} catch (IOException e) {}
    //             if(textPane.getText().equals("已发送图片")){
    //             paint();
    //             }
           
    //      
    //               ImageIcon imageIcon=new ImageIcon(textField_1.getText());
    //               image=imageIcon.getImage();
    //       
    //              g.drawImage(image, 0, 0, this);
    //          textPane.paint(g);
      // 原waitfordata()的代码。
      }
    }
    new ThreadForData().start(); 
      

  11.   

    几个问题:首先:
    public ThreadForData() {
      Thread th=new Thread(); // 这句话是多余的,而且没意义。ThreadForData本身就已经是Thread了。
    }
    其次:
    fos必须在while循环后立即close掉,因为你后面还要用它来显示图片的。
    while(fis.available()>0){
      fos.write(fis.read());
    }
    fos.close(); // 必须立即执行,而不是等到全部运行完毕。
    执行后尝试调用下updateUI,类似:
    textPane.insertIcon(new ImageIcon(textField_1.getText()));
    textPane.setCaretPosition(dic.getLength());
    textPane.updateUI(); // 刷新
    对了,你保存到磁盘的图片,有没有试过用什么图片查看器打开看看是否正常?
      

  12.   

    您好,服务器的保存图片地址是由textField_1的内容来获取的
    textField_1 = new JTextField();
    textField_1.setBounds(246, 485, 195, 21);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);
    textField_1.setText("f:\\asd.jpg");
      

  13.   

    那你修改:textField_1 的取值不就好了?比如用UUID来作为文件名。
    如果你完全不在乎文件名的话,也可以用这个函数:File tmp = File.createTempFile(x,x,x);
      

  14.   

    还有一个重要的问题,就是现在发送图片的时候,发送过去第一张图片能显示出来,然后我再去发送多一张图片,并且改了路径的名称,再去按发送图片这个按钮,但是,之前第一张的图片就消失了(或者说)被第二张的图片覆盖显示了,现在并不能做到像QQ那样,能在同一个聊天框里每次发送的图片都能显示,每发一次图片就会覆盖之前的图片显示,所以现在的情况变成了只能发送一张图片
    下面是服务器的代码
    class ThreadForData extends Thread {
      public ThreadForData() {
     
      }
      public void run() { 
      ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;// 这个函数的定义和参数绝对不能修改
      try {
                ss=new ServerSocket(8888);
               
                while (true) {
                    s = ss.accept();
                    
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(textField_1.getText()));
                  
                    fis=new FileInputStream(a);
                   
                    while(fis.available()>0){
                        fos.write(fis.read());
                       
                    }
                   fos.close();
                  
                  textPane.insertIcon(new ImageIcon(textField_1.getText()));
           textPane.setCaretPosition(dic.getLength());
                   
               
    //                
                   
                  
                  
                  
                                 
                }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
                
                if(fis!=null)try{fis.close();}catch (IOException e) {}
                if(s!=null)try {s.close();} catch (IOException e) {}
                if(ss!=null)try {ss.close();} catch (IOException e) {}
               
                textPane.insertIcon(new ImageIcon(textField_1.getText()));
           textPane.setCaretPosition(dic.getLength());
    (textField_1.getText());
      }
    }
    new ThreadForData().start(); 
      

  15.   

    多次调用insertIcon的话就会连续插入而不会覆盖,是不是同一个textPane对象?先前发送的图片是否还存在?这里看不出第二次图片路径是否不同。
      

  16.   

    谢谢,不能连续发送的问题已经解决了,现在还有一个问题就是,怎样能每次接收到图片之后,都自动换行,我做了很久,只能第一次换行,之后每次发的图片都挤在一起,不能自动换行,请各位高手指教
     try {
                ss=new ServerSocket(8888);
               
                while (true) {
                    s = ss.accept();
                    
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(baocunlj.getText()));
                  
                    fis=new FileInputStream(a);
                   
                    while(fis.available()>0){
                        fos.write(fis.read());
                       
                    }
                   fos.close();
    textPane.setText("\n");//用来换行,可是好像这样做不行
                  textPane.insertIcon(new ImageIcon(baocunlj.getText()));
           textPane.setCaretPosition(dic.getLength());
           // 这两句是为了能在接收完图片之后能显示图片        acd++;
           baocunlj.setText("d:\\"+acd+".jpg");
                }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
      

  17.   

    之前连续发送是什么原因?JTextPane这样是默认自动换行的
    因为宽度默认是固定的,高度可以通过滚动条拓展//Create a text pane.         
    JTextPane textPane = createTextPane();         
    JScrollPane paneScrollPane = new JScrollPane(textPane);         paneScrollPane.setVerticalScrollBarPolicy(                         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);         
    paneScrollPane.setPreferredSize(new Dimension(250, 155));         paneScrollPane.setMinimumSize(new Dimension(10, 10)); 
      

  18.   

    textPane.setText("\n");//用来换行,可是好像这样做不行这样做是把textPane整个内容都替换成了"\n",应该是追加一个换行才对,getText()+"\n"
      

  19.   

    我试了一下不知道这样对不对,但是出来的结果就是,先是正常显示第一张图片,然后,接收第二张的时候就会自动清空一次textPane,然后再显示第二张,第一张图片就直接被清空了
     public void run() { 
      ServerSocket ss=null;
            Socket s=null;
            FileOutputStream fos=null;
            ObjectInputStream ois=null;
            FileInputStream fis=null;// 这个函数的定义和参数绝对不能修改
      try {
                ss=new ServerSocket(8888);
               
                while (true) {
                    s = ss.accept();
                    
                    ois = new ObjectInputStream(s.getInputStream());
                    File a=(File)ois.readObject();
                    fos = new FileOutputStream(new File(baocunlj.getText()));
                  
                    fis=new FileInputStream(a);
                   
                    while(fis.available()>0){
                        fos.write(fis.read());
                       
                    }
                   fos.close();
                   textPane.setText(textPane.getText()+"\n");//我这样写貌似会先清空第一张图片,再放进第二张图片的样子
                  textPane.insertIcon(new ImageIcon(baocunlj.getText()));
           textPane.setCaretPosition(dic.getLength());
           // 这两句是为了能在接收完图片之后能显示图片        acd++;
           baocunlj.setText("f:\\"+acd+".jpg");
                }
             
            }
            catch (Exception e) {
            
                e.printStackTrace();
            }
      

  20.   

    getText可能只能得到text,图片之类的未必能包括其中,关于这个,你还是得找一下JTextPane的用法详细先研究一下http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TextComponentDemoProject/src/components/TextComponentDemo.java
      

  21.   

    TextSamplerDemohttp://docs.oracle.com/javase/tutorial/uiswing/examples/components/index.html#TextComponentDemotry {
        // Get the text pane's document
        JTextPane textPane = new JTextPane();
        StyledDocument doc = (StyledDocument)textPane.getDocument();    // The image must first be wrapped in a style
        Style style = doc.addStyle("StyleName", null);
        StyleConstants.setIcon(style, new ImageIcon("imagefile"));    // Insert the image at the end of the text
        doc.insertString(doc.getLength(), "ignored text", style);
    } catch (BadLocationException e) {
    }