package com.notebook;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.ScrollPaneConstants;
public class NoteBookFrame extends JFrame implements ActionListener {
JMenuBar menuBar = null;
JMenu menuF = null;
JMenu menuE = null;
JMenu menuO = null;
JMenu menuH = null;
JMenuItem menuItemNew = null;
JMenuItem menuItemOpen = null;
JMenuItem menuItemSave = null;
JMenuItem menuItemClose = null;
JMenuItem menuItemCopy = null;
JMenuItem menuItemPaste = null;
JMenuItem menuItemCut = null;
JMenuItem menuItemAll = null;
JMenuItem menuItemDate = null;
JMenuItem menuItemW = null;
JMenuItem menuItemF = null;
JMenuItem menuIteH = null;
JTextArea textArea = null;
JScrollPane jsp = null;
JFileChooser fileChooser = null;
Toolkit toolKit = null;
Clipboard clipboard = null;
public NoteBookFrame() {
this.setTitle("记事本");
this.setBackground(Color.white);
this.setSize(600, 600);
fileChooser = new JFileChooser();
// 获取默认工具包
toolKit = Toolkit.getDefaultToolkit();
// 获取系统 Clipboard 的一个实例,该 Clipboard 可作为本机平台提供的剪贴板工具的接口
clipboard = toolKit.getSystemClipboard();
menuBar = new JMenuBar();
menuF = new JMenu("文件");
menuItemNew = new JMenuItem("新建");
menuItemOpen = new JMenuItem("打开");
menuItemSave = new JMenuItem("保存");
menuItemClose = new JMenuItem("关闭");
menuE = new JMenu("编辑");
menuItemCopy = new JMenuItem("复制");
menuItemPaste = new JMenuItem("粘贴");
menuItemCut = new JMenuItem("剪切");
menuItemAll = new JMenuItem("全选");
menuItemDate = new JMenuItem("日期/时间");
menuO = new JMenu("设置");
menuItemW = new JMenuItem("自动换行");
menuItemF = new JMenuItem("字体");
menuH = new JMenu("帮助");
menuIteH = new JMenuItem("帮助主题");
menuItemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
KeyEvent.CTRL_MASK, false));
menuItemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,
KeyEvent.CTRL_MASK, false));
menuItemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
KeyEvent.CTRL_MASK, false));
menuItemClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,
KeyEvent.CTRL_MASK, false));
menuItemCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
KeyEvent.CTRL_MASK, false));
menuItemPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,
KeyEvent.CTRL_MASK, false));
menuItemCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
KeyEvent.CTRL_MASK, false));
menuItemAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A,
KeyEvent.CTRL_MASK, false));
menuItemDate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D,
KeyEvent.CTRL_MASK, false));
menuItemW.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W,
KeyEvent.CTRL_MASK, false));
menuItemF.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F,
KeyEvent.CTRL_MASK, false));
menuIteH.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_H,
KeyEvent.CTRL_MASK, false));
menuItemOpen.setIcon(new ImageIcon("open.gif"));
menuItemNew.setIcon(new ImageIcon("new.gif"));
menuItemSave.setIcon(new ImageIcon("save.gif"));
menuItemCopy.setIcon(new ImageIcon("copy.gif"));
menuItemPaste.setIcon(new ImageIcon("paste.gif"));
menuItemCut.setIcon(new ImageIcon("cut.gif"));
menuF.add(menuItemNew);
menuF.addSeparator();
menuF.add(menuItemOpen);
menuF.addSeparator();
menuF.add(menuItemSave);
menuF.addSeparator();
menuF.add(menuItemClose);
menuBar.add(menuF);
menuE.add(menuItemCopy);
menuE.addSeparator();
menuE.add(menuItemPaste);
menuE.addSeparator();
menuE.add(menuItemCut);
menuE.addSeparator();
menuE.add(menuItemDate);
menuE.addSeparator();
menuE.add(menuItemAll);
menuBar.add(menuE);
menuO.add(menuItemW);
menuO.addSeparator();
menuO.add(menuItemF);
menuBar.add(menuO);
menuH.add(menuIteH);
menuBar.add(menuH);
textArea = new JTextArea();
jsp = new JScrollPane(textArea);
// 设置垂直滚动条
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// 换行
textArea.setLineWrap(true);
this.add(jsp, BorderLayout.CENTER);
this.add(menuBar, BorderLayout.NORTH);
this.add(jsp);
menuItemNew.addActionListener(this);
menuItemOpen.addActionListener(this);
menuItemSave.addActionListener(this);
menuItemClose.addActionListener(this);
menuItemCopy.addActionListener(this);
menuItemPaste.addActionListener(this);
menuItemCut.addActionListener(this);
menuItemDate.addActionListener(this);
menuItemAll.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
// 打开
if (e.getSource() == menuItemOpen) {
fileChooser.setFileFilter(new MyFilter());
fileChooser.showOpenDialog(this);
File fileName = fileChooser.getSelectedFile();
if (fileName != null) {
openFile(fileName);
}
// 新建
} else if (e.getSource() == menuItemNew) {
textArea.setText("");
// 保存
} else if (e.getSource() == menuItemSave) {
fileChooser.setFileFilter(new MyFilter());
fileChooser.showSaveDialog(this);
File fileName = fileChooser.getSelectedFile();
if (fileName != null) {
writeFile(fileName);
}
// 关闭
} else if (e.getSource() == menuItemClose) {
System.exit(0);
// 复制
} else if (e.getSource() == menuItemCopy) {
// 得到选择的文字
String chooserText = textArea.getSelectedText();
// 建立传输文件的StringSelection对象
StringSelection strSelection = new StringSelection(chooserText);
// 将传输的对象放到剪切板中
clipboard.setContents(strSelection, null);
// 粘贴
} else if (e.getSource() == menuItemPaste) {
// 得到放在剪切板中复制的内容
Transferable textPaste = clipboard.getContents(this);
String text = "";
if (textPaste == null) {
return;
}
// getTransferData()返回一个对象,该对象表示将要被传输的数据
try {
text = (String) textPaste
.getTransferData(DataFlavor.stringFlavor);
} catch (UnsupportedFlavorException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 用给定的新文本替换从指示的起始位置到结尾位置的文本
textArea.replaceRange(text, textArea.getSelectionStart(),
textArea.getSelectionEnd());
// 剪切
} else if (e.getSource() == menuItemCut) {
// 得到选择的文字
String chooserText = textArea.getSelectedText();
// 建立传输文件的StringSelection对象
StringSelection strSelection = new StringSelection(chooserText);
// 将传输的对象放到剪切板中
clipboard.setContents(strSelection, null);
// 用给定的新文本替换从指示的起始位置到结尾位置的文本
textArea.replaceRange("", textArea.getSelectionStart(),
textArea.getSelectionEnd());
// 全选
} else if (e.getSource() == menuItemAll) {
textArea.selectAll();
// 日期/时间
} else if (e.getSource() == menuItemDate) {
Date date = new Date();
// SimpleFormatter format = new SimpleFormatter();
// Calendar cal = Calendar.getInstance();
textArea.append(date.toString());
}
// 自動換行
// }else if(e.getSource()==menuItemW){
// if(!this.textArea.getLineWrap()){
// textArea.setLineWrap(true);
// }else{
// textArea.setLineWrap(false);
// }
// }
}
// 打开文件
public void openFile(File fileName) {
try {
FileReader readIn = new FileReader(fileName);
// 得到文件的大小
int size = (int) fileName.length();
int charsRead = 0;
char[] byt = new char[size];
// 判断此流是否已经准备好用于读取
while (readIn.ready())
// 将字符读入数组中的某一部分
// byt - 目标缓冲区
// charsRead - 从其处开始存储字符的偏移量
// size - charsRead - 要读取的最大字符数
charsRead += readIn.read(byt, charsRead, size - charsRead);
readIn.close();
// 将byte[]转为String
// byt - 要解码为字符的 byte
// 0 - 要解码的第一个 byte 的索引
// charsRead - 要解码的 byte 数
textArea.setText(new String(byt, 0, charsRead));
} catch (Exception e) {
e.getMessage();
}
}
// 保存文件
public void writeFile(File fileName) {
try {
FileWriter write = new FileWriter(fileName);
write.write(textArea.getText());
write.close();
} catch (Exception e) {
e.getMessage();
}
}
}
package com.notebook;
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class MyFilter extends FileFilter{
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
String name = f.getName().toLowerCase();
if(f.isDirectory()){
return true;
}
if(name.endsWith(".jpg")||name.endsWith(".txt")||name.endsWith(".png")){
return true;
}
return false;
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.jpg/*.txt/*.png";
}
}
三、Run.java
package com.notebook;
public class Run {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
NoteBookFrame frame = new NoteBookFrame();
frame.setVisible(true);
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。