menus[i].add(optionofmenu[i][j]);
optionofmenu[i][j].addActionListener(action);
}
}
this.setJMenuBar(menu);
}
ActionListeneraction=newActionListener(){
publicvoidactionPerformed(ActionEvente){
Stringname=e.getActionCommand();
JMenuItemMI=(JMenuItem)e.getSource();
if("新建".equals(name)){
content.setText("");
file=null;
}elseif("打开".equals(name)){
if(file!
=null)openfile.setSelectedFile(file);
intreturnVal=openfile.showOpenDialog(mynotepad.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=openfile.getSelectedFile();
unfold();
}
}elseif("保存".equals(name)){
if(file!
=null)openfile.setSelectedFile(file);
intreturnVal=openfile.showSaveDialog(mynotepad.this);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=openfile.getSelectedFile();
saving();
}
}elseif("退出".equals(name)){
mynotepadf=newmynotepad();
ints=JOptionPane.showConfirmDialog(f,"退出?
","退出",JOptionPane.YES_NO_OPTION);
if(s==JOptionPane.YES_OPTION)
System.exit(0);
}elseif("剪切".equals(name)){
content.cut();
}elseif("复制".equals(name)){
content.copy();
}elseif("粘贴".equals(name)){
content.paste();
}elseif("颜色".equals(name)){
color=JColorChooser.showDialog(mynotepad.this,"",color);
content.setForeground(color);
}elseif("关于".equals(name)){
about.setSize(300,150);
about.show();
}
}
};
voidsaving(){
try{
FileWriterWritef=newFileWriter(file);
Writef.write(content.getText());
Writef.close();
}
catch(Exceptione){e.printStackTrace();}
}
voidunfold(){
try{
FileReaderReadf=newFileReader(file);
intlen=(int)file.length();
char[]buffer=newchar[len];
Readf.read(buffer,0,len);
Readf.close();
content.setText(newString(buffer));
}catch(Exceptione){e.printStackTrace();}
}
voidinitAboutDialog(){
about.setLayout(newGridLayout(3,1));
about.getContentPane().setBackground(Color.white);
about.getContentPane().add(newJLabel("我的记事本程序"));
about.getContentPane().add(newJLabel("制作者:
liuhui"));
about.getContentPane().add(newJLabel("2010年6月"));
about.setModal(true);
about.setSize(100,100);
about.setLocation(250,170);
}
;
}
publicclassNotepad{
publicstaticvoidmain(Stringargs[]){
mynotepadnoted=newmynotepad();
noted.addWindowListener(newWindowAdapter(){
});
noted.setTitle("我的记事本程序");
noted.setSize(640,320);
noted.show();
noted.setLocation(150,100);
}
}