📄 notebook.java
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class notebook {
public static void main(String[] args)
{ note f=new note("我的记事本");
f.setLocation(100,50);
}
}
class note extends JFrame implements ActionListener
{ String str,strnext,path,fname;
JPanel mainpane;
JFileChooser choose = new JFileChooser();
Dialog find,replace;
JTextField findtxt,repltxt;
Font newfont;
JButton findenter,replb;
JTextArea txt1;
File newfiles;
JPopupMenu popm;
JMenu m1,m2,m3,m4,m6;
JMenuItem m61,m62,m26,m271,m34,p_copy,p_cut,p_paste,p_del,p_choose;
int startp,endp,nexttemp,newstartp,newendp;
note(String s)
{super(s);
JMenuBar mb=new JMenuBar();
fname=null;
findenter=new JButton("确定");
findenter.addActionListener(this);
replb=new JButton("确定");
replb.addActionListener(this);
mainpane=(JPanel)this.getContentPane();
mainpane.setLayout(new GridLayout(1,1));
txt1=new JTextArea("",20,61);
txt1.addMouseListener(new handlemouse(this));
txt1.setFont(new Font("宋体",Font.PLAIN,18));
mainpane.add(txt1, BorderLayout.CENTER);
JScrollPane sll = new JScrollPane();
mainpane.add("Center", sll); ;
sll.getViewport().add(txt1);
popm=new JPopupMenu();
p_copy=new JMenuItem("复制 ");
p_copy.addActionListener(this);
KeyStroke keycopyp=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);
p_copy.setAccelerator(keycopyp);
p_cut=new JMenuItem("剪切 ");
p_cut.addActionListener(this);
KeyStroke keycutp=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK);
p_cut.setAccelerator(keycutp);
p_paste=new JMenuItem("粘贴 ");
p_paste.addActionListener(this);
KeyStroke keypp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);
p_paste.setAccelerator(keypp);
p_del=new JMenuItem("删除 ");
p_del.addActionListener(this);
KeyStroke keydelp=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0);
p_del.setAccelerator(keydelp);
p_choose=new JMenuItem("全选 ");
p_choose.addActionListener(this);
KeyStroke keychoose=KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK);
p_choose.setAccelerator(keychoose);
popm.add(p_cut);
popm.add(p_copy);
popm.add(p_paste);
popm.add(p_del);
popm.add(p_choose);
txt1.add(popm);
m1=new JMenu("文件(F)");
m1.setMnemonic('F');
JMenuItem m10=new JMenuItem("新建 ");
m10.addActionListener(this);
KeyStroke keynew=KeyStroke.getKeyStroke(KeyEvent.VK_N,Event.CTRL_MASK);
m10.setAccelerator(keynew);
JMenuItem m11=new JMenuItem("打开...");
m11.addActionListener(this);
KeyStroke keyopen=KeyStroke.getKeyStroke(KeyEvent.VK_O,Event.CTRL_MASK);
m11.setAccelerator(keyopen);
JMenuItem m12=new JMenuItem("保存 ");
m12.addActionListener(this);
KeyStroke keysave=KeyStroke.getKeyStroke(KeyEvent.VK_S,Event.CTRL_MASK);
m12.setAccelerator(keysave);
JMenuItem m13=new JMenuItem("另存为...");
m13.addActionListener(this);
JMenuItem m14=new JMenuItem("退出 ");
m14.addActionListener(this);
KeyStroke keyexit=KeyStroke.getKeyStroke(KeyEvent.VK_F4,Event.ALT_MASK);
m14.setAccelerator(keyexit);
m2=new JMenu("编辑(E)");
m2.setMnemonic('E');
JMenuItem m21=new JMenuItem("复制 ");
m21.addActionListener(this);
KeyStroke keycopy=KeyStroke.getKeyStroke(KeyEvent.VK_C,Event.CTRL_MASK);
m21.setAccelerator(keycopy);
JMenuItem m22=new JMenuItem("剪切 ");
m22.addActionListener(this);
KeyStroke keycut=KeyStroke.getKeyStroke(KeyEvent.VK_X,Event.CTRL_MASK);
m22.setAccelerator(keycut);
JMenuItem m23=new JMenuItem("粘贴 ");
m23.addActionListener(this);
KeyStroke keyp=KeyStroke.getKeyStroke(KeyEvent.VK_V,Event.CTRL_MASK);
m23.setAccelerator(keyp);
JMenuItem m24=new JMenuItem("删除 ");
m24.addActionListener(this);
KeyStroke keydel=KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0);
m24.setAccelerator(keydel);
JMenuItem m25=new JMenuItem("查找 ");
m25.addActionListener(this);
KeyStroke keyfind=KeyStroke.getKeyStroke(KeyEvent.VK_F,Event.CTRL_MASK);
m25.setAccelerator(keyfind);
m26=new JMenuItem("查找下一个 ");
m26.addActionListener(this);
KeyStroke keyfn=KeyStroke.getKeyStroke(KeyEvent.VK_F3,0);
m26.setAccelerator(keyfn);
m26.setEnabled(false);
JMenuItem m27=new JMenuItem("替换 ");
m27.addActionListener(this);
KeyStroke keyrepl=KeyStroke.getKeyStroke(KeyEvent.VK_H,Event.CTRL_MASK);
m27.setAccelerator(keyrepl);
m271=new JMenuItem("替换下一个");
m271.setEnabled(false);
m271.addActionListener(this);
KeyStroke keyrepn=KeyStroke.getKeyStroke(KeyEvent.VK_F6,0);
m271.setAccelerator(keyrepn);
JMenuItem m28=new JMenuItem("全选 ");
m28.addActionListener(this);
KeyStroke keyall=KeyStroke.getKeyStroke(KeyEvent.VK_A,Event.CTRL_MASK);
m28.setAccelerator(keyall);
JMenuItem m29=new JMenuItem("日期/时间 ");
m29.addActionListener(this);
m3=new JMenu("格式(O)");
m3.setMnemonic('O');
JMenu m31=new JMenu("字体样式 ");
JMenuItem m311=new JMenuItem("正常 ");
m311.addActionListener(this);
JMenuItem m312=new JMenuItem("粗体 ");
m312.addActionListener(this);
JMenuItem m313=new JMenuItem("斜体 ");
m313.addActionListener(this);
JMenu m32=new JMenu("字体大小 ");
JMenuItem m321=new JMenuItem("最大 ");
m321.addActionListener(this);
JMenuItem m322=new JMenuItem("较大 ");
m322.addActionListener(this);
JMenuItem m323=new JMenuItem("适中 ");
m323.addActionListener(this);
JMenuItem m324=new JMenuItem("较小 ");
m324.addActionListener(this);
JMenuItem m325=new JMenuItem("最小 ");
m325.addActionListener(this);
JMenuItem m33=new JMenuItem("字体颜色 ");
m33.addActionListener(this);
m6=new JMenu("帮助(H)");
m6.setMnemonic('H');
m62=new JMenuItem("关于 ");
m62.addActionListener(this);
m1.add(m10); m1.add(m11); m1.add(m12); m1.add(m13); m1.addSeparator(); m1.add(m14);
m2.add(m22); m2.add(m21); m2.add(m23); m2.add(m24); m2.addSeparator(); m2.add(m25);
m2.add(m26); m2.add(m27); m2.add(m271); m2.addSeparator(); m2.add(m28);
m2.add(m29); m3.add(m31); m31.add(m311); m31.add(m312); m31.add(m313);
m3.add(m32); m32.add(m321); m32.add(m322); m32.add(m323); m32.add(m324);
m32.add(m325); m3.add(m33); m6.add(m62); mb.add(m1); mb.add(m2); mb.add(m3); mb.add(m6);
this.setJMenuBar(mb);
setVisible(true);
pack();
show();
this.addWindowListener(new L123(this));
}
public void actionPerformed(ActionEvent p)
{
if(p.getActionCommand()=="新建 ")
{
fname=null;
txt1.setText("");
}
if(p.getActionCommand()=="打开...")
{
try {
if(this.choose.APPROVE_OPTION==this.choose.showOpenDialog(this))
{
path=this.choose.getSelectedFile().getPath();
fname=this.choose.getSelectedFile().getName();
File file=new File(path);
int flength=(int)file.length();
FileReader fReader=new FileReader(file);
char[] data=new char[flength];
fReader.read(data,0,flength);
txt1.setText(new String(data));
txt1.setCaretPosition(0);
}
}
catch(IOException e)
{}
}
if(p.getActionCommand()=="保存 ")
{
if(fname==null)
{ othersave(); }
try {
File savefile=new File(path);
savefile.createNewFile();
FileWriter fw=new FileWriter(savefile);
fw.write(txt1.getText());
fw.close();
}
catch(IOException e)
{}
}
if(p.getActionCommand()=="另存为...")
{othersave();}
if(p.getActionCommand()=="退出 ")
{exit();}
if(p.getActionCommand()=="复制 ")
{ txt1.copy(); }
if(p.getActionCommand()=="剪切 ")
{ txt1.cut(); }
if(p.getActionCommand()=="粘贴 ")
{ txt1.paste(); }
if(p.getActionCommand()=="删除 ")
{
txt1.replaceSelection("");
}
if(p.getActionCommand()=="查找 ")
{find=new Dialog(this,"查找");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
findtxt=new JTextField(7);
p1.add(new JLabel("输入要查找字符:"));
p1.add(findtxt);
p2.add(findenter);
find.add("Center",p1);
find.add("South",p2);
find.setSize(200,100);
find.show();
}
if(p.getSource()==findenter)
{
if(findtxt.getText().equals(""))
{find.dispose();}
else if(!findtxt.getText().equals(""))
{ find.dispose();
str=txt1.getText();
startp=str.indexOf(findtxt.getText());
endp=startp+findtxt.getText().length();
txt1.select(startp,endp);
m26.setEnabled(true);
newendp=endp;
notfindmethod();
}
}
if(p.getActionCommand()=="查找下一个 ")
{
nexttemp=newendp;
String strall=txt1.getText();
txt1.select(nexttemp,strall.length());
strnext=txt1.getSelectedText();
newstartp=strnext.indexOf(findtxt.getText())+nexttemp;
newendp=newstartp+findtxt.getText().length();
txt1.select(newstartp,newendp);
notfindmethod();
}
if(p.getActionCommand()=="替换 ")
{m271.setEnabled(true);
replace=new Dialog(this,"替换");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
findtxt=new JTextField(7);
repltxt=new JTextField(7);
p1.add(new JLabel("输入原始字符:"));
p1.add(findtxt);
p2.add(new JLabel("输入替换字符:"));
p2.add(repltxt);
p3.add(replb);
replace.add("North",p1);
replace.add("Center",p2);
replace.add("South",p3);
replace.setSize(200,150);
replace.show();
}
if(p.getSource()==replb)
{
replace.dispose();
str=txt1.getText();
startp=str.indexOf(findtxt.getText());
endp=startp+findtxt.getText().length();
txt1.replaceRange(repltxt.getText(),startp,endp);
newendp=endp;
}
if(p.getActionCommand()=="替换下一个")
{nexttemp=newendp;
String strall=txt1.getText();
txt1.select(nexttemp,strall.length());
strnext=txt1.getSelectedText();
newstartp=strnext.indexOf(findtxt.getText())+nexttemp;
newendp=newstartp+findtxt.getText().length();
txt1.select(newstartp,newendp);
notfindmethod();
txt1.replaceRange(repltxt.getText(),newstartp,newendp);
}
if(p.getActionCommand()=="全选 ")
{txt1.selectAll(); }
if(p.getActionCommand()=="日期/时间 ")
{ int inpoint=txt1.getCaretPosition();
Date dt=new Date();
String strdate=dt.toLocaleString();
txt1.insert(strdate,inpoint);}
if(p.getActionCommand()=="正常 ")
{ newfont=txt1.getFont();
int size=newfont.getSize();
txt1.setFont(new Font("宋体",Font.PLAIN,size)); }
if(p.getActionCommand()=="粗体 ")
{ newfont=txt1.getFont();
int size=newfont.getSize();
txt1.setFont(new Font("宋体",Font.BOLD,size)); }
if(p.getActionCommand()=="斜体 ")
{ newfont=txt1.getFont();
int size=newfont.getSize();
txt1.setFont(new Font("宋体",Font.ITALIC,size)); }
if(p.getActionCommand()=="最大 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,35)); }
if(p.getActionCommand()=="较大 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,30)); }
if(p.getActionCommand()=="适中 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,18)); }
if(p.getActionCommand()=="较小 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,16)); }
if(p.getActionCommand()=="最小 ")
{ newfont=txt1.getFont();
int sty=newfont.getStyle() ;
txt1.setFont(new Font("宋体",sty,14)); }
if(p.getActionCommand()=="字体颜色 ")
{
JColorChooser jColor=new JColorChooser();
Color fcolor=txt1.getForeground();
txt1.setForeground( jColor.showDialog(txt1,"选择字体颜色",fcolor));
}
if(p.getActionCommand()=="关于 ")
{
JLabel prompt=new JLabel("author:Wang Runlan ",JLabel.CENTER);
JOptionPane.showMessageDialog(null,prompt,"我的记事本",JOptionPane.INFORMATION_MESSAGE);
}
}
public void othersave()
{
if(choose.APPROVE_OPTION==choose.showSaveDialog(this))
{
path=choose.getSelectedFile().getPath();
newfiles=new File(path);
fname=choose.getSelectedFile().getName();;
try {
newfiles=new File(path);
newfiles.createNewFile();
FileWriter fw=new FileWriter(newfiles);
fw.write(txt1.getText());
fw.close();
}catch(IOException e)
{}
}
}
public void notfindmethod()
{
if(!txt1.getSelectedText().equals(findtxt.getText()))
{ txt1.setCaretPosition(0);
JOptionPane.showMessageDialog(null,"查找不到对应的字符!","查找错误",JOptionPane.ERROR_MESSAGE);
m26.setEnabled(false); }
}
public void exit()
{ int value;
String[] qq={"确定", "取消"};
value=JOptionPane.showOptionDialog(null, "确定退出吗?请注意保存文件!", "退出程序?",
JOptionPane.DEFAULT_OPTION, JOptionPane.QUESTION_MESSAGE,
null, qq, qq[0]);
if(value==JOptionPane.YES_OPTION)
System.exit(0);
else if(value==JOptionPane.NO_OPTION)
{ return; }
}
}
class myhelp extends JFrame
{
}
class L123 extends WindowAdapter
{note fx;
L123(note fxx)
{fx=fxx;}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
class L123help extends WindowAdapter
{myhelp fx;
L123help(myhelp fxx)
{fx=fxx;}
public void windowClosing(WindowEvent e)
{
fx.dispose();
}
}
class handlemouse extends MouseAdapter
{
note fx;
handlemouse(note fxx)
{fx=fxx;}
public void mouseReleased(MouseEvent n)
{
if(n.isPopupTrigger())
fx.popm.show((Component)n.getSource(),n.getX(),n.getY());
}
}