package yjq.com.cn;
/*
* WriterReaderBoard.java
*/
import java.awt.datatransfer.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.awt.BorderLayout;
import java.awt.FileDialog;
import java.awt.Font;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.border.TitledBorder;
public class WriterReaderBoard extends JFrame{
private static final long serialVersionUID = 1L;
Clipboard clipboard =null;
private StringBuffer url = null;
private JFrame f;
private JMenu menu1;
private JMenu menu2;
private JMenu menu3;
private JMenuBar menuBar1;
private JMenuItem menuNewFile;
private JMenuItem menuOpen;
private JMenuItem menuSave;
private JMenuItem menuSaveAs;
private JMenuItem menuExit;
private JMenuItem menuCut;
private JMenuItem menuCopy;
private JMenuItem menuPaste;
private JTextArea textArea;
private JMenuItem menuSelectAll;
private JMenuItem menuUndo;
private JMenuItem menuAbout;
/** WriteBoard 构造方法*/
public WriterReaderBoard() {
clipboard = this.getToolkit().getSystemClipboard();
this.initComponents();
}
private void initComponents() {
url = new StringBuffer();
textArea = new JTextArea();
menuBar1 = new JMenuBar();
menu1 = new JMenu();
menuNewFile = new JMenuItem();
menuOpen = new JMenuItem();
menuSave = new JMenuItem();
menuSaveAs = new JMenuItem();
menuExit = new JMenuItem();
menu2 = new JMenu();
menuCut = new JMenuItem();
menuCopy = new JMenuItem();
menuPaste = new JMenuItem();
menuSelectAll = new JMenuItem();
menuUndo = new JMenuItem();
setTitle("无标题 - YJQ记事本");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
exitForm(evt); }});
add(textArea,BorderLayout.CENTER);
menu1.setText("Menu");
menu1.setMnemonic(KeyEvent.VK_M);
menuNewFile.setText("新建(N)");
menuNewFile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
newText(evt);}});
menuNewFile.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_N,
InputEvent.CTRL_MASK));
menu1.add(menuNewFile);
menuOpen.setText("打开(O)...");
menuOpen.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
open(evt);
}});
menuOpen.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_O,
InputEvent.CTRL_MASK));
menu1.add(menuOpen);
menuSave.setText("保存(S)");
menuSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuSaveActionPerformed(evt);
}});
menuSave.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_S,
InputEvent.CTRL_MASK));
menu1.add(menuSave);
menuSaveAs.setText("另存为(A)...");
menuSaveAs.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt){
menuSaveAsActionPerformed(evt);
}});
menu1.add(menuSaveAs);
menuExit.setText("退出");
menuExit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
menu1.add(menuExit);
menuBar1.add(menu1);
menu2.setText("编辑(E)");
menu2.setMnemonic(KeyEvent.VK_E);
menuCut.setText("剪贴(X)");
menuCut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuCutActionPerformed(evt);
}});
menuCut.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_X,
InputEvent.CTRL_MASK));
menuCopy.setText("复制(C)");
menuCopy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuCopyActionPerformed(evt);
}});
menuCopy.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_C,
InputEvent.CTRL_MASK));
menuPaste.setText("粘贴(V)");
menuPaste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuPasteActionPerformed(evt);
}});
menuPaste.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_V,
InputEvent.CTRL_MASK));
menuSelectAll.setText("全选(A)");
menuSelectAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuAllSelectActionPerformed(evt);
}});
menuSelectAll.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_A,
InputEvent.CTRL_MASK));
menuUndo.setText("撤销(Z)");
menuUndo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
menuUndoActionPerformed(evt);
}
});
menuUndo.setAccelerator(
KeyStroke.getKeyStroke(
KeyEvent.VK_Z,
InputEvent.CTRL_MASK));
menu2.add(menuCut);
menu2.add(menuCopy);
menu2.add(menuPaste);
menu2.add(menuSelectAll);
menu2.add(menuUndo);
menuBar1.add(menu2);
menu3 = new JMenu("帮助");
menuAbout = new JMenuItem("关于记事本");
menuAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
JOptionPane.showOptionDialog(null,"程序名称:\n 我的记事本 \n"+
"功能简介:读UTF-8编码文档,写文档以UTF-8存储",
"Designer:YJQ",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE, null, null,null
);
}});
menu3.add(menuAbout);
menuBar1.add(menu3);
setJMenuBar(menuBar1);
f= new JFrame();
JScrollPane jp = new JScrollPane();
textArea = new JTextArea();
textArea.setFont(new Font("宋体", Font.BOLD, 13));
textArea.setLineWrap(true);
jp.getViewport().add(textArea);
JPanel pane = new JPanel(new BorderLayout());
pane.add(BorderLayout.NORTH, menuBar1);//添加菜单栏
pane.add(BorderLayout.CENTER,jp);//添加文本编辑区域
f.getContentPane().add(pane);
int WIDTH = 600;
int HEIGHT =600;
int w = (Toolkit.getDefaultToolkit().getScreenSize().width-WIDTH)/2;
int h = (Toolkit.getDefaultToolkit().getScreenSize().height-HEIGHT)/2;
f.setBounds(w, h,WIDTH, HEIGHT);
f.setTitle("无标题-我的记事本");
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
private void menuUndoActionPerformed(ActionEvent evt) {
}
private void menuAllSelectActionPerformed(ActionEvent evt) {
}
private void menuSaveAsActionPerformed(ActionEvent evt) {
String fileName = "无标题";
FileDialog fd = new FileDialog(new JFrame(), "保存文本",
FileDialog.SAVE);
fd.setVisible(true);
if (fd.getDirectory() != null && fd.getFile() != null){
url.setLength(0);
url.append(fd.getDirectory()+ fd.getFile());
fileName = fd.getFile();
File file = new File(url.toString().trim());
BufferedWriter out = null;
try{
out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(file),"UTF-8"));
file.createNewFile();
out.append(textArea.getText());
out.flush();
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
f.setTitle(fileName+"-我的记事本");
}
private void menuSaveActionPerformed(ActionEvent evt) {
if (url.length() == 0) {
final FileDialog fd = new FileDialog(new JFrame(), "保存文件",
FileDialog.SAVE);
fd.setVisible(true);
if (fd.getDirectory()!= null && fd.getFile() != null){
url.append(fd.getDirectory()+ fd.getFile());
}
}
File file = new File(url.toString());
BufferedWriter out = null;
try {
out = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(file),"UTF-8"));
file.createNewFile();
System.out.println(textArea.getLineCount());
out.append(textArea.getText());
out.flush();
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
private void menuPasteActionPerformed(ActionEvent evt) {
Transferable contents = clipboard.getContents(this);
DataFlavor flavor = DataFlavor.stringFlavor;
if(contents.isDataFlavorSupported(flavor))
try{
String str;
str=(String)contents.getTransferData(flavor);
textArea.append(str);
}catch(Exception e){}
}
private void menuCopyActionPerformed(ActionEvent evt) {
String temp = this.textArea.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text,null);
}
private void menuCutActionPerformed(ActionEvent evt) {
String temp = this.textArea.getSelectedText();
StringSelection text = new StringSelection(temp);
clipboard.setContents(text,null);
int start = textArea.getSelectionStart();
int end = textArea.getSelectionEnd();
textArea.replaceRange("",start,end);
}
private void open(ActionEvent evt) {
String fileName = "无标题";
final FileDialog fd = new FileDialog(new JFrame(), "查找文件",
FileDialog.LOAD);
fd.setVisible(true);
if (fd.getDirectory() != null && fd.getFile() != null) {
fileName = fd.getFile();
textArea.setText("");
url.setLength(0);
url.append(fd.getDirectory()+ fd.getFile());
System.out.println(url.toString());
File file = new File(url.toString().trim());
String s = null;
StringBuffer sb = new StringBuffer();
if (file.exists()) {
System.out.println("该文件存在");
try {
FileInputStream fIn = new FileInputStream(file);
BufferedReader br =
new BufferedReader(
new InputStreamReader(
fIn,"UTF-8"));
while ((s = br.readLine()) != null) {
sb.append(s+"\r\n");
}
textArea.append(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
else System.out.println("该文件不存在!");
}
f.setTitle(fileName+"我的记事本");
}
private void newText(ActionEvent evt) {
f.setTitle("无标题-我的记事本");
this.textArea.setText("");
url.setLength(0);
}
private void exitForm(WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
new WriterReaderBoard();
}
}
撤销功能有待完善。。。还有字符编码问题!!!如果是用该软件读自己用该软件写的文档没有问题,如果是读doc或者txt非"UTF-8"字符集的,就可能会出现中文乱码,有待完善,网上说有jar包可以实现,不过不是JDK自带的。