我用java编写的记事本代码,分享给大家

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.IOException;

import javax.swing.*;
import javax.swing.event.UndoableEditListener;

@SuppressWarnings("serial")
public class NoteBook extends JFrame{
JPanel contentPane;
Font a=new Font("TimeRoman",Font.PLAIN,16);//设置字体
JMenuBar notepad=new JMenuBar();
JMenu File=new JMenu("文件");
JMenu Edit=new JMenu("编辑");
JMenu format=new JMenu("格式");
JMenu backcolor=new JMenu("背景色");//分级菜单
JMenu Help=new JMenu("帮助");
JMenuItem newfile=new JMenuItem("新建");
JMenuItem openfile=new JMenuItem("打开");
JMenuItem savefile=new JMenuItem("保存");
JMenuItem exit=new JMenuItem("退出");
JMenuItem clear=new JMenuItem("清空");
JMenuItem paste=new JMenuItem("粘贴");
JMenuItem copy=new JMenuItem("复制");
JMenuItem cut=new JMenuItem("剪切");
JMenuItem all=new JMenuItem("全选");
JMenuItem about=new JMenuItem("关于");

JMenuItem red=new JMenuItem("红色");//","橙","黄","","青","蓝","紫","黑","灰","白","粉红"
JMenuItem green=new JMenuItem("绿色");
JMenuItem yellow=new JMenuItem("黄色");
JMenuItem orange=new JMenuItem("橙色");
JMenuItem cyan=new JMenuItem("青色");
JMenuItem blue=new JMenuItem("蓝色");
JMenuItem purpo=new JMenuItem("紫色");
JMenuItem black=new JMenuItem("黑色");
JMenuItem gray=new JMenuItem("灰色");
JMenuItem white=new JMenuItem("白色");
JMenuItem pink=new JMenuItem("粉红");
JMenuItem morecolor=new JMenuItem("更多颜色");

JScrollPane scrollpane;
public JTextArea textarea;
protected JFrame jnb;



public NoteBook(){
try{
setDefaultCloseOperation(EXIT_ON_CLOSE);
notebookInit();
}catch(Exception e){
e.printStackTrace();
}
}

private void notebookInit() throws Exception{
//设置字体
File.setFont(a); File.setForeground(Color.blue);
Edit.setFont(a); Edit.setForeground(Color.blue);
format.setFont(a); format.setForeground(Color.blue);
Help.setFont(a); Help.setForeground(Color.blue);
backcolor.setFont(a); backcolor.setForeground(Color.MAGENTA);
newfile.setFont(a); newfile.setForeground(Color.MAGENTA);
openfile.setFont(a); openfile.setForeground(Color.MAGENTA);
savefile.setFont(a); savefile.setForeground(Color.MAGENTA);
exit.setFont(a); exit.setForeground(Color.MAGENTA);
clear.setFont(a); clear.setForeground(Color.MAGENTA);
paste.setFont(a); paste.setForeground(Color.MAGENTA);
cut.setFont(a); cut.setForeground(Color.MAGENTA);
all.setFont(a); all.setForeground(Color.MAGENTA);
copy.setFont(a); copy.setForeground(Color.MAGENTA);
about.setFont(a); about.setForeground(Color.MAGENTA);
red.setFont(a); red.setForeground(Color.MAGENTA);
orange.setFont(a); oran

ge.setForeground(Color.MAGENTA);
yellow.setFont(a); yellow.setForeground(Color.MAGENTA);
green.setFont(a); green.setForeground(Color.MAGENTA);
cyan.setFont(a); cyan.setForeground(Color.MAGENTA);
blue.setFont(a); blue.setForeground(Color.MAGENTA);
purpo.setFont(a); purpo.setForeground(Color.MAGENTA);
black.setFont(a); black.setForeground(Color.MAGENTA);
gray.setFont(a); gray.setForeground(Color.MAGENTA);
white.setFont(a); white.setForeground(Color.MAGENTA);
pink.setFont(a); pink.setForeground(Color.MAGENTA);
morecolor.setFont(a); morecolor.setForeground(Color.MAGENTA);
contentPane=(JPanel)getContentPane();
contentPane.setLayout(new BorderLayout());
//设置菜单
this.setJMenuBar(notepad);
setSize(new Dimension(800,600));
setTitle("记事本");
notepad.add(File);
notepad.add(Edit);
notepad.add(format);
notepad.add(Help);
File.add(newfile); File.addSeparator();//添加分割线
File.add(openfile); File.addSeparator();
File.add(savefile); File.addSeparator();
File.add(exit);
Edit.add(clear); Edit.addSeparator();
Edit.add(paste); Edit.addSeparator();
Edit.add(copy); Edit.addSeparator();
Edit.add(cut); Edit.addSeparator();
Edit.add(all);
format.add(backcolor);//菜单添加子菜单
backcolor.add(red); backcolor.addSeparator();
backcolor.add(orange); backcolor.addSeparator();
backcolor.add(yellow); backcolor.addSeparator();
backcolor.add(green); backcolor.addSeparator();
backcolor.add(cyan); backcolor.addSeparator();
backcolor.add(blue); backcolor.addSeparator();
backcolor.add(purpo); backcolor.addSeparator();
backcolor.add(black); backcolor.addSeparator();
backcolor.add(gray); backcolor.addSeparator();
backcolor.add(white); backcolor.addSeparator();
backcolor.add(pink); backcolor.addSeparator();
backcolor.add(morecolor);
Help.add(about);
//分级菜单


//添加文本框和滚动面板
textarea=new JTextArea(30,70);
scrollpane=new JScrollPane();
scrollpane.setViewportView(textarea);//scrollpane.getViewport().add(textarea);
add(scrollpane);

//添加动作
newfile.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new NoteBookNew();
}
});

openfile.addActionListener(new open_action(this));
savefile.addActionListener(new save_action(this));

exit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(-1);
}
});


clear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setText("");
}
});

all.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.selectAll();
}
});




copy.addActionListener(new ActionLis

tener() {//copyItem就是拷贝那个菜单按钮对象
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String text = textarea.getSelectedText();//这里从一个JTextArea拷贝选中的文本
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
}
});

cut.addActionListener(new ActionListener() {//copyItem就是拷贝那个菜单按钮对象
public void actionPerformed(ActionEvent e) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
String text = textarea.getSelectedText();//这里从一个JTextArea(oArea)拷贝选中的文本
StringSelection selection = new StringSelection(text);
clipboard.setContents(selection, null);
textarea.cut();
}
});

paste.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor flavor = DataFlavor.stringFlavor;
if (clipboard.isDataFlavorAvailable(flavor)) {
try {
String text = (String) clipboard.getData(flavor);
if (textarea.getSelectedText() == null) {//iArea是粘贴目标的那个JTextArea
int caretposion=textarea.getCaretPosition();//没有选中的文本,取得光标位置
textarea.insert(text, caretposion);//插入字符
} else {
textarea.replaceSelection(text);//如果选中了文本,则替换选中的文本
}
} catch (UnsupportedFlavorException ex) {
ex.printStackTrace();
} catch (IOException e1){
e1.printStackTrace();
}
}


}
});


red.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.RED);
}
});

orange.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.ORANGE);
}
});

yellow.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.yellow);
}
});

green.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.green);
}
});

cyan.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.cyan);
}
});

blue.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackgro

und(Color.blue);
}
});

purpo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.getHSBColor(299, (float)0.48, (float)0.66));
}
});

black.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.black);
}
});

gray.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.gray);
}
});

white.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.white);
}
});

pink.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.pink);
}
});
//调用颜色对话框并返回颜色
morecolor.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color c=JColorChooser.showDialog(morecolor, "颜色选择器", Color.white);
textarea.setBackground(c);
}
});


about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
AboutDialog cd=new AboutDialog(null);
cd.setSize(200,150);
cd.setModal(true);
cd.pack();
cd.setVisible(true);
}

});



//弹出式菜单

JPopupMenu popupMenu=new JPopupMenu();
JMenuItem paste1=new JMenuItem("粘贴");
JMenuItem copy1=new JMenuItem("复制");
JMenuItem cut1=new JMenuItem("剪切");
JMenuItem all1=new JMenuItem("全选");
JMenu backcolor1=new JMenu("背景色");
JMenuItem red1=new JMenuItem("红色");//","橙","黄","","青","蓝","紫","黑","灰","白","粉红"
JMenuItem green1=new JMenuItem("绿色");
JMenuItem yellow1=new JMenuItem("黄色");
JMenuItem orange1=new JMenuItem("橙色");
JMenuItem cyan1=new JMenuItem("青色");
JMenuItem blue1=new JMenuItem("蓝色");
JMenuItem purpo1=new JMenuItem("紫色");
JMenuItem black1=new JMenuItem("黑色");
JMenuItem gray1=new JMenuItem("灰色");
JMenuItem white1=new JMenuItem("白色");
JMenuItem pink1=new JMenuItem("粉红");
final JMenuItem morecolor1=new JMenuItem("更多颜色");

popupMenu.add(all1); popupMenu.addSeparator();
popupMenu.add(copy1); popupMenu.addSeparator();
popupMenu.add(paste1); popupMenu.addSeparator();
popupMenu.add(cut1); popupMenu.addSeparator();
popupMenu.add(backcolor1);
backcolor1.add(red1); backcolor1.addSeparator();
backcolor1.add(orange1); backcolor1.addSeparator();
backcolor1.add(yellow1); backcolor1.addSeparator();
backcolor1.add(green1); backcolor1.addSeparator();
backcolor1.add(cyan1); backcolor1.addSep

arator();
backcolor1.add(blue1); backcolor1.addSeparator();
backcolor1.add(purpo1); backcolor1.addSeparator();
backcolor1.add(black1); backcolor1.addSeparator();
backcolor1.add(gray1); backcolor1.addSeparator();
backcolor1.add(white1); backcolor1.addSeparator();
backcolor1.add(pink1); backcolor1.addSeparator();
backcolor1.add(morecolor1);


backcolor1.setFont(a); backcolor1.setForeground(Color.darkGray);
paste1.setFont(a); paste1.setForeground(Color.darkGray);
cut1.setFont(a); cut1.setForeground(Color.darkGray);
all1.setFont(a); all1.setForeground(Color.darkGray);
copy1.setFont(a); copy1.setForeground(Color.darkGray);
red1.setFont(a); red1.setForeground(Color.darkGray);
orange1.setFont(a); orange1.setForeground(Color.darkGray);
yellow1.setFont(a); yellow1.setForeground(Color.darkGray);
green1.setFont(a); green1.setForeground(Color.darkGray);
cyan1.setFont(a); cyan1.setForeground(Color.darkGray);
blue1.setFont(a); blue1.setForeground(Color.darkGray);
purpo1.setFont(a); purpo1.setForeground(Color.darkGray);
black1.setFont(a); black1.setForeground(Color.darkGray);
gray1.setFont(a); gray1.setForeground(Color.darkGray);
white1.setFont(a); white1.setForeground(Color.darkGray);
pink1.setFont(a); pink1.setForeground(Color.darkGray);
morecolor1.setFont(a); morecolor1.setForeground(Color.darkGray);

//设置弹出菜单
textarea.setComponentPopupMenu(popupMenu);

all1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.selectAll();
}
});

copy1.addActionListener(new ActionListener() {//copyItem就是拷贝那个菜单按钮对象
public void actionPerformed(ActionEvent e) {
textarea.copy(); //系统自带的复制类
}
});

cut1.addActionListener(new ActionListener() {//copyItem就是拷贝那个菜单按钮对象
public void actionPerformed(ActionEvent e) {

textarea.cut();
}
});

paste1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.paste();

}
});

red1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.RED);
}
});

orange1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.ORANGE);
}
});

yellow1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.yellow);
}
});

green1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textar

ea.setBackground(Color.green);
}
});

cyan1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.cyan);
}
});

blue1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.blue);
}
});

purpo1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.getHSBColor(299, (float)0.48, (float)0.66));
}
});

black1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.black);
}
});

gray1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.gray);
}
});

white1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.white);
}
});

pink1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
textarea.setBackground(Color.pink);
}
});

morecolor1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Color c1=JColorChooser.showDialog(morecolor1, "背景色选择器", Color.white);
textarea.setBackground(c1);
}

});

}



public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable(){
public void run(){
NoteBook nb=new NoteBook();
nb.pack();
nb.setVisible(true);
nb.setLocationRelativeTo(null);//文本框在屏幕中央出现

}
});
}
}




import javax.swing.JFrame;


@SuppressWarnings("serial")
public class NoteBookNew extends NoteBook{

NoteBookNew(){
setTitle("新建 ");
this.setVisible(true);
}

public static void main(String[] args){
NoteBookNew n=new NoteBookNew();
n.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}



}


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class open_action implements ActionListener{
private NoteBook adaptee;
open_action(NoteBook adaptee){
this.adaptee=adaptee;
}
public void actionPerformed(ActionEvent e){
JFileChooser openf=new JFileChooser();
//设置记事本支持格式
openf.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt,*.java,*.asm,*.cmd,*.bat,*.ini,*.lrc)", "txt","java","asm","cmd","bat","lrc","ini"));
openf.

showOpenDialog(adaptee);
File a=openf.getSelectedFile();
String path=a.getPath();//得到文件路径
//String part=a.separator;//获取系统分隔符'\'(92)
System.out.println(path);//path名字为单斜杆表示路径,需要改为双斜
/*
int num[]=new int[200];//获取'\'位置
int b=0,count=0;
for(int i=0;iif((b=path.indexOf((int)'\\',i))!=-1)//找到了从i开始第一次出现'\'的位置
{num[b]=b;//返回'\'的位置
i=b;
count++;
}
}
for(int i=0;iSystem.out.println("num["+i+"]="+num[i]);

StringBuffer path2=new StringBuffer(path);
System.out.println(path2);
for(int i=0;i<200;i++){//防止因插入斜杆是字符串变长
if((num[i]!=-1)&&(num[i]!=0))
path2.insert(num[i]+1, "\\");//在'\'处插入'\',注意单斜杆用双斜杆表示
}

String path3=path2.toString();

System.out.println(path3);
*/

String result="";
String line=null;

try{
//URL fileUrl=open_action.class.getResource(path3);//
//System.out.println(fileUrl);//
FileReader ir=new FileReader(path);//BufferedReader br=new BufferedReader(new InputStreamReader(fileUrl.openStream()));
BufferedReader br=new BufferedReader(ir);
while((line=br.readLine())!=null){
result+=line+"\n";
}
br.close();
ir.close();
}catch(IOException e1){
System.out.println("Error:"+e1.getMessage());
}
adaptee.textarea.setText(result);

}

}

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;


public class save_action implements ActionListener {
private NoteBook adaptee;
public save_action(NoteBook adaptee) {
this.adaptee=adaptee;
}

public void actionPerformed(ActionEvent e) {
JFileChooser savef=new JFileChooser();
savef.setFileFilter(new FileNameExtensionFilter("文本文件(*.txt,*.java,*.asm,*.cmd,*.bat,*.ini,*.lrc)", "txt","java","asm","cmd","bat","lrc","ini"));
savef.showSaveDialog(adaptee);
File a=savef.getSelectedFile();
String path=a.getPath();//得到保存路径
/*
int num[]=new int[200];//获取'\'位置
int b=0,count=0;
for(int i=0;iif((b=path.indexOf((int)'\\',i))!=-1)//找到了从i开始第一次出现'\'的位置
{num[b]=b;//返回'\'的位置
i=b;
count++;
}
}


for(int i=0;iSystem.out.println("num["+i+"]="+num[i]);

StringBuffer path2=new StringBuffer(path);
for(int i=0;i<200;i++){//防止因插入斜杆是字符串变长
if((num[i]!=-1)&&(num[i]!=0))
path2.insert(num[i]+1, "\\");//在'\'处插入'\',注意单斜杆用双斜杆表示
}
System.out.println(path2);
String path3=path2.toString();
System.out.println(path3);
*/
try{
FileWriter fw=new FileWriter(path);
BufferedWriter bw=new BufferedWriter(fw);//PrintWriter out=new PrintWriter(fw)
bw.write(adaptee.textarea.getText());//out.print(..);
bw.close();
fw.close();
}catch(IOException e1){
System.out.println("Error:"+e1.getMessage());
}

}

}


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextArea;

@SuppressWarnings("serial")
class AboutDialog extends JDialog{
JLabel about=new JLabel("关于该记事本的说明:",JLabel.CENTER);
JTextArea jTextArea=new JTextArea(20,42);

public AboutDialog(Frame parent){
super(parent);
setTitle("关于");

Font a=new Font("TimeRoman",Font.BOLD,18);
about.setFont(a);
about.setForeground(Color.blue);

Font f=new Font("宋体",Font.ITALIC,16);

jTextArea.setText("\n\n\n\n本java程序用于记事,若出现问题,敬请谅解,谢谢支持!\n\n\n作者:合肥师范学院 10级计算机系 计算机软件 一班 王辉东\n\n\n\n制作时间:2012年,4月,28号\n\n联系方式:\n\t QQ:1182583214\n\t手机:138******** ");
jTextArea.setFont(f);
jTextArea.setBackground(new Color(200,252,92));
jTextArea.setEditable(false);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(about,BorderLayout.NORTH);
getContentPane().add(jTextArea,BorderLayout.AFTER_LINE_ENDS);

}
}

相关文档
最新文档