package com.yidu.demo;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.swing.ImageIcon;
import javax.swing.JColorChooser;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextPane;
import javax.swing.KeyStroke;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
import javax.swing.text.StyleConstants.ColorConstants;
import javax.swing.text.StyleConstants.FontConstants;
import sun.font.FontFamily;
public class MainFrame extends JFrame {
JTextPane editorPane=new JTextPane();
Socket socket;
public MainFrame(){
JMenuBar bar=new JMenuBar();
this.setJMenuBar(bar);
JMenu menuFile=new JMenu("编辑");
bar.add(menuFile);
JMenuItem itemSend=new JMenuItem("发送");
menuFile.add(itemSend);
try {
socket=new Socket("127.0.0.1",1000);
} catch (UnknownHostException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
itemSend.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
OutputStream os= socket.getOutputStream();
ObjectOutputStream objectOutputStream=new ObjectOutputStream(os);
objectOutputStream.writeObject(editorPane);
objectOutputStream.flush();
} catch (UnknownHostException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
JMenuItem itemGet=new JMenuItem("得到内容");
menuFile.add(itemGet);
itemGet.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Document document= editorPane.getDocument();
editorPane.insertIcon(new ImageIcon("MessageReading.gif"));
}
});
JMenuItem itemColor=new JMenuItem("颜色");
itemColor.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
JColorChooser colorChooser=new JColorChooser();
Color color= colorChooser.showDialog(null, "字体颜色", Color.BLACK);
Document document= editorPane.getDocument();
try {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.Foreground, color);
Font font=new Font("隶书",Font.BOLD,30);
aset=sc.addAttribute(aset, StyleConstants.Family, font.getFamily());
aset=sc.addAttribute(aset, StyleConstants.FontSize, 30);
int start= editorPane.getSelectionStart();
int end=editorPane.getSelectionEnd();
String str= document.getText(start,end-start);
document.remove(start, end-start);
document.insertString(start, str, aset);
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
menuFile.add(itemColor);
itemColor.setMnemonic('C');
//设置快捷键
itemColor.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.ALT_MASK));
this.add(editorPane);
editorPane.setText("测试");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* @param args
*/
public static void main(String[] args) {
MainFrame mainFrame=new MainFrame();
mainFrame.setSize(400, 300);
mainFrame.setVisible(true);
}
}