java记事本.docx
《java记事本.docx》由会员分享,可在线阅读,更多相关《java记事本.docx(19页珍藏版)》请在冰点文库上搜索。
java记事本
java记事本
界面展示:
代码:
packagecn.hlbrc.jsb;
importjavax.swing.SwingUtilities;
importjava.awt.BorderLayout;
importjavax.swing.JPanel;
importjavax.swing.JFrame;
importjava.awt.Dimension;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjavax.swing.JFileChooser;
importjavax.swing.JMenuBar;
importjavax.swing.JMenu;
importjavax.swing.JMenuItem;
importjavax.swing.JOptionPane;
importjavax.swing.JTextArea;
importjavax.swing.JTextField;
importjavax.swing.KeyStroke;
//importjavax.xml.crypto.Date;
importjava.awt.Rectangle;
importjava.awt.event.InputEvent;
importjava.awt.event.KeyEvent;
importjava.awt.Color;
importjava.awt.Font;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
publicclassjsbextendsJFrame{
privatestaticfinallongserialVersionUID=1L;
privateJPaneljContentPane=null;
privateJMenuBarjJMenuBar=null;
privateJMenum1=null;
privateJMenum2=null;
privateJMenum3=null;
privateJMenum4=null;
privateJMenuItemt1=null;
privateJMenuItemt2=null;
privateJMenuItemt3=null;
privateJMenuItemt4=null;
privateJMenuItemt5=null;
privateJMenuItemt6=null;
privateJMenuItemt7=null;
privateJMenuItemt8=null;
privateJMenuItemt9=null;
privateJMenuItemt10=null;
privateJMenuItemt11=null;
Stringstr;
privateJMenuItemjMenuItem=null;
privateJMenuItemt12=null;
privateJMenuItemt13=null;
privateJMenuItemt14=null;
privateJMenuItemt15=null;
privateJTextAreatf1=null;
FileReaderfr;
booleanflag=false;
/**
*ThismethodinitializesjJMenuBar
*
*@returnjavax.swing.JMenuBar
*/
privateJMenuBargetJJMenuBar(){
if(jJMenuBar==null){
jJMenuBar=newJMenuBar();
jJMenuBar.add(getM1());
jJMenuBar.add(getM2());
jJMenuBar.add(getM3());
jJMenuBar.add(getM4());
}
returnjJMenuBar;
}
/**
*Thismethodinitializesm1
*@return
*
*@returnjavax.swing.JMenu
*/
privateJMenugetM1(){
if(m1==null){
m1=newJMenu();
m1.setText("文件");
m1.add(getT1());
m1.add(getT2());
m1.add(getT3());
m1.add(getT4());
}
returnm1;
}
privateJMenugetM2(){
if(m2==null){
m2=newJMenu();
m2.setText("编辑");
m2.add(getT5());
m2.add(getT6());
m2.add(getT7());
m2.add(getT8());
m2.add(getT9());
m2.add(getT10());
m2.add(getT11());
}
returnm2;
}
privateJMenugetM3(){
if(m3==null){
m3=newJMenu();
m3.setText("格式");
m3.add(getT12());
m3.add(getT13());
}
returnm3;
}
privateJMenugetM4(){
if(m4==null){
m4=newJMenu();
m4.setText("系统");
m4.add(getJMenuItem());
m4.add(getT14());
m4.add(getT15());
}
returnm4;
}
/**
*Thismethodinitializest1
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT1(){
if(t1==null){
t1=newJMenuItem();
t1.setText("新建");
t1.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
if(!
tf1.getText().equals(""))
if(JOptionPane.showConfirmDialog(null,
"是否保存文件内容","保存文件",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
JFileChooserjf=newJFileChooser();
jf.showSaveDialog(jsb.this);
tf1.setText("");
}
}
});
}
returnt1;
}
/**
*Thismethodinitializest2
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT2(){
if(t2==null){
t2=newJMenuItem();
t2.setText("打开");
t2.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
JFileChooserjf=newJFileChooser();
jf.showOpenDialog(jsb.this);
Filef=jf.getSelectedFile();
try{
fr=newFileReader(f);
}catch(FileNotFoundExceptione1){
//TODO自动生成catch块
e1.printStackTrace();
}
intdata;
Stringstr=newString();
try{
while((data=fr.read())!
=-1)
str=str+(char)data;
}catch(IOExceptione1){
//TODO自动生成catch块
e1.printStackTrace();
}
tf1.setText(str);
}
});
}
returnt2;
}
/**
*Thismethodinitializest3
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT3(){
if(t3==null){
t3=newJMenuItem();
t3.setText("保存");
t3.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
JFileChooserjf=newJFileChooser();
jf.showSaveDialog(jsb.this);
Filef=jf.getSelectedFile();
}
});
}
returnt3;
}
/**
*Thismethodinitializest4
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT4(){
if(t4==null){
t4=newJMenuItem();
t4.setText("另存为");
t4.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
JFileChooserjf=newJFileChooser();
jf.showSaveDialog(jsb.this);
}
});
}
returnt4;
}
/**
*Thismethodinitializesj1
*
*@returnjavax.swing.JTextArea
*/
/**
*Thismethodinitializesm2
*
*@returnjavax.swing.JMenu
*/
/**
*Thismethodinitializest5
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT5(){
if(t5==null){
t5=newJMenuItem();
t5.setText("复制");
t5.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
str=tf1.getSelectedText();
t5.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,
InputEvent.CTRL_MASK));
}
});
}
returnt5;
}
/**
*Thismethodinitializest6
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT6(){
if(t6==null){
t6=newJMenuItem();
t6.setText("贴粘");
t6.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
tf1.setText(tf1.getText()+str);
//tf1.get
t6.setAccelerator(
KeyStroke.getKeyStroke(KeyEvent.VK_V,
InputEvent.CTRL_MASK));
}
});
}
returnt6;
}
/**
*Thismethodinitializest7
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT7(){
if(t7==null){
t7=newJMenuItem();
t7.setText("剪切");
t7.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
t7.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
InputEvent.CTRL_MASK));
str=tf1.getSelectedText();
tf1.replaceSelection("");//从text中删除被选取文本
}
});
}
returnt7;
}
/**
*Thismethodinitializest8
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT8(){
if(t8==null){
t8=newJMenuItem();
t8.setText("删除");
}
returnt8;
}
/**
*Thismethodinitializest9
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT9(){
if(t9==null){
t9=newJMenuItem();
t9.setText("全选");
}
returnt9;
}
/**
*Thismethodinitializest10
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT10(){
if(t10==null){
t10=newJMenuItem();
t10.setText("查找");
}
returnt10;
}
/**
*Thismethodinitializest11
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT11(){
if(t11==null){
t11=newJMenuItem();
t11.setText("替换");
}
returnt11;
}
/**
*Thismethodinitializesm3
*
*@returnjavax.swing.JMenu
*/
/**
*Thismethodinitializesm4
*
*@returnjavax.swing.JMenu
*/
/**
*ThismethodinitializesjMenuItem
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetJMenuItem(){
if(jMenuItem==null){
jMenuItem=newJMenuItem();
}
returnjMenuItem;
}
/**
*Thismethodinitializest12
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT12(){
if(t12==null){
t12=newJMenuItem();
t12.setText("颜色");
}
returnt12;
}
/**
*Thismethodinitializest13
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT13(){
if(t13==null){
t13=newJMenuItem();
t13.setText("字体");
}
returnt13;
}
/**
*Thismethodinitializest14
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT14(){
if(t14==null){
t14=newJMenuItem();
}
returnt14;
}
/**
*Thismethodinitializest15
*
*@returnjavax.swing.JMenuItem
*/
privateJMenuItemgetT15(){
if(t15==null){
t15=newJMenuItem();
t15.setText("退出");
t15.addActionListener(newjava.awt.event.ActionListener(){
publicvoidactionPerformed(java.awt.event.ActionEvente){
System.exit(0);//退出
}
});
}
returnt15;
}
/**
*Thismethodinitializestf1
*
*@returnjavax.swing.JTextArea
*/
privateJTextAreagetTf1(){
if(tf1==null){
tf1=newJTextArea();
tf1.setAutoscrolls(true);
}
returntf1;
}
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODO自动生成方法存根
SwingUtilities.invokeLater(newRunnable(){
publicvoidrun(){
jsbthisClass=newjsb();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
*Thisisthedefaultconstructor
*/
publicjsb(){
super();
initialize();
}
/**
*Thismethodinitializesthis
*
*@returnvoid
*/
privatevoidinitialize(){
this.setSize(477,295);
this.setJMenuBar(getJJMenuBar()