日期:2014-05-20  浏览次数:20671 次

求救,我做了一个发送图片的发送端和接收端,但是可能是因为接收端的一些问题导致不能让JTextPane按路径显示图片,在这里求救各位JAVA高手
我的代码如下,代码实现了界面,一个发送端和一个接收端
import java.awt.EventQueue;


public class QQ<FontAttrib> extends JFrame {

JFrame frame;
private JScrollPane scrollPane_1 ; // 滚动
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();
 
/**
* 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);



textPane.setBounds(0, 0, 442, 291);
frame.getContentPane().add(textPane);
dic = textPane.getStyledDocument();
textPane.setCaretPosition(dic.getLength()); // 设置插入位置
textPane.insertIcon(new ImageIcon("f:\\ds"+a1+sj+".jpg")); // 插入图片
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, 432, 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())); // 插入图片

 
}
});

JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(440, 301, -444, 146);
frame.getContentPane().add(scrollPane);
button.setBounds(10, 457, 93, 23);
frame.getContentPane().add(button);





JButton button_1 = new JButton("\u6E05\u7A7A");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textPane_1.setText("");