这段代码,我是先定义了接收端的路径,然后发送到指定的地方,我试过加多一个按钮的功能能直接从路径中把已经发送到的图片显示出来,可是这样很不方便,请各位高手教一教在下,怎样可以接受端的一方一接受到对方的图片保存到指定路径后,直接显示出来,不需要另外一个按钮做跳板!而且这段代码只能发送一个图片,因为还没交上循环代码。还不能刷出一张新的图片。
以下是代码,希望各位高手帮忙修改一下,在下已经苦恼了3到4个星期了,而且快要到交作业的期限了,请求各位高手帮帮忙
以下是代码,希望各位高手帮忙修改一下,在下已经苦恼了3到4个星期了,而且快要到交作业的期限了,请求各位高手帮帮忙
解决方案 »
- freetts语音识别空指针异常
- 五子棋
- 我写的一个小程序,不知道为什么老是抛出数组越界的异常,请各位高手指正.
- DNS编程问题
- 请帮忙:hibernat下怎么对sqlserver的image字段读写?比如存图片进去,读一个文件出来
- 为什么我的Resultset中没有数据?
- 为了这个问题,我在出200分。(呵呵,想出200分确不让,只好解决以后在开一100分的贴)
- Graphics2D 的一个问题
- 在哪里可以下载扩展包,javax.mail.*,javax.mail.internet.*!下来先得分!谢谢
- java平台
- io什么时候乱码问题
- 为什么这个程序中的睡眠状态不执行,应该打印出来是0514233241
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;
}
}
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())); // 插入图片
}
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) {}
}
我之前也通过多加的一个接收的按钮,接收按钮里面调用了调用本地图片显示的方法,路径也是获取文本框的内容信息为路径的,最终也可以正常显示图片。按以上的写法就要连按两次运行才能弹出窗口出来,如果各位不明白我说的是什么情况的话,可以运行上面的代码试试!
可是现在我想不通过按钮这个跳板来显示图片,却不能正常显示,每次启动程序,都要按两次运行,才能弹出聊天窗口出来。
其实这里,还有一个问题没解决的就是如何通过循环来刷新出每一张图片,而不会每次都覆盖原先的图片。在这里我打算先能在正常运行的情况下能显示一张图片之后再去探究循环刷出新图片的问题在这里请教各位高手了
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())); // 插入图片
}
}
而第二次因为端口占用的问题而未得到执行,因此界面得以显示了,但也可能因此造成了楼主所说的问题
试着将此部分代码放到新线程中执行吧
请给些具体的指示
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) {}}
,可是现在还是显示不到图片,在这里为我之前的错误判断而道歉,但是现在用了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);
//
}
});
}
class ThreadForData extends Thread {
public ThreadForData(...) {
// 构造函数,可以用来传递你所需的某些参数,比如窗体对象,然后将其保存至成员变量中
}
public void run() { // 这个函数的定义和参数绝对不能修改
// 原waitfordata()的代码。
}
}启动时可以: new ThreadForData(...).start();
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();
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(); // 刷新
对了,你保存到磁盘的图片,有没有试过用什么图片查看器打开看看是否正常?
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");
如果你完全不在乎文件名的话,也可以用这个函数:File tmp = File.createTempFile(x,x,x);
下面是服务器的代码
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();
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();
}
因为宽度默认是固定的,高度可以通过滚动条拓展//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));
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();
}
// 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) {
}