我的代码如下,代码实现了界面,一个发送端和一个接收端
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("");
}
});
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)
{
} 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) {
textPane.setText("127.0.0.1"+sj+":"+"\n");
sendatatupian();
a1++;
}
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();
textPane_1.setText("");
//textPane.insertIcon(new ImageIcon("f:\\ds"+a1+sj+".jpg".toString()));
} 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);
}
public void paint() {
if(textPane.getText().equals("已发送图片")){
textPane.setCaretPosition(dic.getLength()); // 设置插入位置
textPane.insertIcon(new ImageIcon("f:\\ds"+a1+sj+".jpg")); // 插入图片
}
}
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("f:\\ds"+a1+sj+".jpg"));
fis=new FileInputStream(a);
while(fis.available()>0){
fos.write(fis.read());
}
a1++;
}
}
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;
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("");
}
});
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)
{
} 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) {
textPane.setText("127.0.0.1"+sj+":"+"\n");
sendatatupian();
a1++;
}
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();
textPane_1.setText("");
//textPane.insertIcon(new ImageIcon("f:\\ds"+a1+sj+".jpg".toString()));
} 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);
}
public void paint() {
if(textPane.getText().equals("已发送图片")){
textPane.setCaretPosition(dic.getLength()); // 设置插入位置
textPane.insertIcon(new ImageIcon("f:\\ds"+a1+sj+".jpg")); // 插入图片
}
}
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("f:\\ds"+a1+sj+".jpg"));
fis=new FileInputStream(a);
while(fis.available()>0){
fos.write(fis.read());
}
a1++;
}
}
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;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;public class MainClass extends JFrame {
public static void main(String[] args) {
new MainClass();
} public MainClass() {
this.setTitle("Picture Application");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = new JPanel();
ImageIcon pic = new ImageIcon("your.jpg");
panel1.add(new JLabel(pic));
this.add(panel1);
this.pack();
this.setVisible(true);
}
}
public void waitfordata() {//服务器端,发送图片接收方
ServerSocket ss=null;
Socket s=null;
FileOutputStream fos=null;
ObjectInputStream ois=null;
FileInputStream fis=null;
File f=new File("f:\\ds"+a1+sj+".jpg");
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("f:\\ds"+a1+sj+".jpg"));
fis=new FileInputStream(a);
while(fis.available()>0){
fos.write(fis.read());
}
a1++;
}
}
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) {}
// textPane.setText("已连接服务器");
if(fos!=null&&fis!=null&&s!=null&&ss!=null){
textPane.setText("127.0.0.1");
ImageIcon pic = new ImageIcon("f:\\ds"+a1+sj+".jpg");
textPane.add(new JLabel(pic));// 该把这里改成这样吗?
}
}
可是我改了之后没什么反应,可能是我领悟错了,请高手教一教
import java.awt.BorderLayout;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextPane;public class PaneInsertionMethods { public static void main(String[] args) { final JTextPane pane = new JTextPane(); pane.replaceSelection("text");
pane.insertIcon(new ImageIcon("imageName.gif"));
pane.insertComponent(new JButton("Click Me")); JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(pane, BorderLayout.CENTER);
frame.setSize(360, 180);
frame.setVisible(true);
}
}
textPane.insertIcon(new ImageIcon("ds020129192024.jpg"));
这样写的指定路径来显示就能接收到
可是因为我还要发送图片,而且发送的图片不能被覆盖,所以必须定义一个int类来循环,不断制造新的图片不被覆盖,还用上读取时间 GregorianCalendar的代码,所以路径的名字就变成有f:\\"ds"+int值+读取时间的变量+".jpg"组成了,所以显示路径的句子就变成
textPane.insertIcon(new ImageIcon("ds"+a1+sj+".jpg"));
这里解释一下,a1是一个int值的变量,每当接收图片一次就自加1,来更新出一张图片,而sj是GregorianCalendar类来读取时间定义的一个变量,
整体的接收端代码为
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("e:\\JAVA\\Text\\ds"+a1+sj+".jpg"));
fis=new FileInputStream(a);
while(fis.available()>0){
fos.write(fis.read());
}
a1++;
}
}
catch (Exception e) {
e.printStackTrace();
}
textPane.setText("127.0.0.1");
textPane.insertIcon(new ImageIcon("ds"+a1+sj+".jpg"));
textPane.insertComponent(new JButton("Click Me"));